<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>Mysql 基本操作 | matrix</title>
    <meta property="og:title" content="Mysql 基本操作 - matrix">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-01-16T14:30:24&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-01-16T14:30:24&#43;08:00'>
        
    <meta name="Keywords" content="黑客技术，WEB安全，让你听懂每个漏洞成因，明白每个技术原理,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="Mysql 基本操作">
        
    <meta name="author" content="matrix">
    <meta property="og:url" content="https://gitmatrix.gitee.io/post/Mysql-%E6%95%B0%E6%8D%AE%E5%BA%93/Mysql-%E6%93%8D%E4%BD%9C/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>


<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://gitmatrix.gitee.io/">
                        matrix
                    </a>
                
                <p class="description">黑客技术，WEB安全，让你听懂每个漏洞成因，明白每个技术原理</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://gitmatrix.gitee.io/">首页</a>
                    
                    <a  href="https://gitmatrix.gitee.io/archives/" title="归档">归档</a>
                    
                    <a  href="https://gitmatrix.gitee.io/about/" title="关于">关于</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#数据库的常见概念">数据库的常见概念</a></li>
      </ul>
    </li>
    <li><a href="#sql语言的分类">SQL语言的分类</a></li>
    <li><a href="#第一章-dql-数据查询语言">第一章 DQL 数据查询语言</a>
      <ul>
        <li></li>
        <li><a href="#14单行函数">1.4、单行函数</a></li>
        <li><a href="#35分组函数">3.5、分组函数</a></li>
        <li><a href="#36分组查询-grop-by">3.6、分组查询 grop by</a></li>
        <li><a href="#37连接查询">3.7、连接查询</a></li>
        <li><a href="#38sql99标准">3.8、sql99标准</a></li>
        <li><a href="#39子查询">3.9、子查询</a></li>
        <li><a href="#310分页查询">3.10、分页查询</a></li>
        <li><a href="#311联合查询">3.11、联合查询</a></li>
      </ul>
    </li>
    <li><a href="#第四章-dml语言">第四章 DML语言</a>
      <ul>
        <li><a href="#41插入语句">4.1、插入语句</a></li>
        <li><a href="#42修改语句">4.2、修改语句</a></li>
        <li><a href="#43删除语句">4.3、删除语句</a></li>
      </ul>
    </li>
    <li><a href="#第五章-ddl语言">第五章 DDL语言</a>
      <ul>
        <li><a href="#51库的管理">5.1、库的管理</a></li>
        <li><a href="#52表的管理">5.2、表的管理</a></li>
        <li><a href="#53数据类型">5.3、数据类型</a></li>
        <li><a href="#54常见约束">5.4、常见约束</a></li>
        <li><a href="#55自增长列">5.5、自增长列</a></li>
      </ul>
    </li>
    <li><a href="#第六章-dcl语言">第六章 DCL语言</a>
      <ul>
        <li><a href="#61创建用户">6.1、创建用户</a></li>
        <li><a href="#62删除用户">6.2、删除用户</a></li>
        <li><a href="#63用户授权">6.3、用户授权</a></li>
        <li><a href="#64撤销授权">6.4、撤销授权</a></li>
        <li><a href="#65刷新授权">6.5、刷新授权</a></li>
        <li><a href="#66查看授权">6.6、查看授权</a></li>
        <li><a href="#67修改密码">6.7、修改密码</a></li>
        <li><a href="#68忘记密码">6.8、忘记密码</a></li>
      </ul>
    </li>
    <li><a href="#第七章-tcl语言">第七章 TCL语言</a>
      <ul>
        <li><a href="#71事务">7.1、事务</a></li>
        <li><a href="#72事务并发读问题">7.2、事务并发（读问题）</a></li>
        <li><a href="#73丢失更新写问题">7.3、丢失更新（写问题）</a></li>
      </ul>
    </li>
    <li><a href="#第八章-高级部分">第八章 高级部分</a>
      <ul>
        <li><a href="#81索引">8.1、索引</a></li>
        <li><a href="#82视图">8.2、视图</a></li>
        <li><a href="#83存储过程">8.3、存储过程</a></li>
        <li><a href="#84函数">8.4、函数</a></li>
        <li><a href="#85触发器">8.5、触发器</a></li>
        <li><a href="#86事件">8.6、事件</a></li>
      </ul>
    </li>
    <li><a href="#第九章-数据备份与恢复">第九章 数据备份与恢复</a>
      <ul>
        <li><a href="#91数据导出">9.1、数据导出</a></li>
        <li><a href="#92数据导入">9.2、数据导入</a></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">Mysql 基本操作</h1>
        </header>
        <date class="post-meta meta-date">
            2021年1月16日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/Mysql'>Mysql</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="clear" style="display: none">
            <div class="toc-article">
                <div class="toc-title">文章目录</div>
            </div>
        </div>
        
        <div class="post-content">
            <h1 id="mysql-基本操作">Mysql 基本操作</h1>
<blockquote>
<h3 id="数据库的常见概念">数据库的常见概念</h3>
<ol>
<li>DBS：数据库系统，数据库系统是一个通称，包括数据库、数据库管理系统、数据库管理人员等，<strong>是最大的范畴</strong></li>
<li>DBMS：数据库管理系统，又称为数据库软件或者数据库产品，用于创建和管理数据库，常见的有MySQL、Oracle、SQL Server</li>
<li>DB：数据库，存储数据的仓库</li>
<li>SQL：结构化查询语言，用于和数据库通信的语言，不是某个数据库软件特有的，而是几乎所有的主流数据库软件通用的语言</li>
</ol>
</blockquote>
<h2 id="sql语言的分类">SQL语言的分类</h2>
<ul>
<li><strong>DQL :   数据查询语言 ： select，form，where</strong></li>
<li><strong>DML ：数据操作语言 ：insert，update，delete</strong></li>
<li><strong>DDL ： 数据定义语言： create，alter （更改），drop，truncate （截取）</strong></li>
<li><strong>DCL :   数据控制语言 ： grant （赋权）, revoke （撤销）</strong></li>
<li><strong>TCL ： 事务控制语言 ： commit （提交）， rollback （回滚）</strong></li>
</ul>
<h2 id="第一章-dql-数据查询语言">第一章 DQL 数据查询语言</h2>
<h4 id="11基础查询">1.1基础查询</h4>
<h5 id="select-查询列表-from-表名">select 查询列表 from 表名;</h5>
<p>语法：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p><strong>特点</strong></p>
<ol>
<li>查询列表可以是字段、常量、函数、表达式</li>
<li>查询结果是一个虚拟表</li>
</ol>
</blockquote>
<h6 id="1查询单个字段">1、查询单个字段</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="2查询多个字段">2、查询多个字段</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span>,<span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span>,<span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名；</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="3查询所有字段">3、查询所有字段</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名；</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="4查询常量">4、查询常量</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">常量值</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">常量值</span>;
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：字符型和日期型的常量值必须用单引号引起来，数值型不需要</p>
</blockquote>
<h6 id="5查询函数">5、查询函数</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>(<span style="color:#a61717;background-color:#e3d2d2">实参列表</span>);

mysql<span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#000;font-weight:bold">select</span> curdate();
<span style="color:#000;font-weight:bold">+</span><span style="color:#998;font-style:italic">------------+
</span><span style="color:#998;font-style:italic"></span><span style="color:#000;font-weight:bold">|</span> curdate()  <span style="color:#000;font-weight:bold">|</span>
<span style="color:#000;font-weight:bold">+</span><span style="color:#998;font-style:italic">------------+
</span><span style="color:#998;font-style:italic"></span><span style="color:#000;font-weight:bold">|</span> <span style="color:#099">2020</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">12</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">24</span> <span style="color:#000;font-weight:bold">|</span>
<span style="color:#000;font-weight:bold">+</span><span style="color:#998;font-style:italic">------------+
</span></code></pre></td></tr></table>
</div>
</div><h6 id="6查询表达式">6、查询表达式</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#099">100</span><span style="color:#000;font-weight:bold">/</span><span style="color:#099">25</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#099">2</span><span style="color:#000;font-weight:bold">*</span><span style="color:#099">64</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="7起别名">7、起别名</h6>
<pre><code class="language-》sql" data-lang="》sql"> SELECT 字段名 AS &quot;别名&quot; FROM 表名;
select 字段名 as '别名' from 表名；
</code></pre><blockquote>
<p>注意：别名可以使用单引号、双引号引起来，当只有一个单词时，可以省略引号，当有多个单词且有空格或特殊符号时，不能省略，<strong>AS可以省略</strong></p>
</blockquote>
<h6 id="8去重复---distinct">8、去重复   distinct</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">DISTINCT</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">distinct</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">from</span>  <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="9做加法">9、做加法</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、</span> <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">数值</span><span style="color:#000;font-weight:bold">+</span><span style="color:#a61717;background-color:#e3d2d2">数值</span>; <span style="color:#a61717;background-color:#e3d2d2">直接运算</span>
<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、</span> <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">字符</span><span style="color:#000;font-weight:bold">+</span><span style="color:#a61717;background-color:#e3d2d2">数值</span>; <span style="color:#a61717;background-color:#e3d2d2">首先先将字符转换为整数，如果转换成功，则继续运算，如果转换失败，则默认为</span><span style="color:#099">0</span><span style="color:#a61717;background-color:#e3d2d2">，然后继续运算</span>
<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、</span> <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">NULL</span><span style="color:#000;font-weight:bold">+</span><span style="color:#a61717;background-color:#e3d2d2">数值</span>; NULL和任何数值参与运算结果都是NULL
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="10补充ifnull函数">10、【补充】ifnull函数</h6>
<blockquote>
<p>ifnull (判断值 ，指定值)  功能：判断某字段或表达式是否为null，如果为null，返回指定的值，否则返回原本的值</p>
</blockquote>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> IFNULL(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>, <span style="color:#a61717;background-color:#e3d2d2">指定值</span>) <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">select</span> ifnull(<span style="color:#a61717;background-color:#e3d2d2">字段名，指定值</span>) <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名；</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="11补充isnull函数">11、【补充】isnull函数</h6>
<blockquote>
<p>功能：判断某字段或表达式是否为null，如果是null，则返回1，否则返回0</p>
</blockquote>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">ISNULL</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>) <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">isnull</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>) <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名；</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="12条件查询">1.2条件查询</h4>
<h5 id="select-查询列表-from-表名-where-筛选条件">select 查询列表 from 表名 where 筛选条件；</h5>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">where</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件；</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、分类</strong></p>
<ol>
<li>条件运算符：&gt;、&gt;=、&lt;、&lt;=、=、&lt;=&gt;、!=、&lt;&gt;</li>
<li>逻辑运算符：and、or、not 或 ！(逻辑非） xor (逻辑异或)</li>
<li>模糊运算符：
<ul>
<li>like：%任意多个字符、_任意单个字符，如果有特殊字符，需要使用escape转义</li>
<li>between and    在两值之间</li>
<li>not between and  不在两值之间</li>
<li>in  在集合中</li>
<li>is null  为空</li>
<li>is not null 不为空</li>
<li>regexp 或者 rlike 正则式匹配</li>
</ul>
</li>
</ol>
<p><strong>三、演示</strong></p>
<h6 id="1--大于">1，&gt;  大于</h6>
<p>1、查询工资&gt;12000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">12000</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="2--大于等于">2，&gt;=  大于等于</h6>
<p>2、查询工资&gt;=14000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&gt;=</span> <span style="color:#099">14000</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="3--小于">3，&lt;  小于</h6>
<p>3、查询工资&lt;12000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#099">12000</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="4--小于于等于">4，&lt;=  小于于等于</h6>
<p>4、查询工资&lt;=14000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&lt;=</span> <span style="color:#099">14000</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="5-等于">5，= 等于</h6>
<p>5、查询员工编号=100的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">100</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="6---强化-判断null">6，&lt;=&gt;   强化= 判断null</h6>
<p>6、查询员工编号&lt;=&gt;100的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">&lt;=&gt;</span> <span style="color:#099">100</span> ;

</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：=只能判断普通类型的数值（<strong>有操作码为NULL时 ，都为null</strong>），而&lt;=&gt;不仅可以判断普通类型的数值还可以判断NULL <strong>(两个操作码均为NULL时，其所得值为1</strong>；<strong>而当一个操作码为NULL时，其所得值为0</strong>)</p>
</blockquote>
<h6 id="7---不等于">7， !=  不等于</h6>
<p>7、查询员工编号!=100的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">!=</span> <span style="color:#099">100</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="8--不等于-推荐使用">8， &lt;&gt; 不等于 （推荐使用）</h6>
<p>8、查询员工编号&lt;&gt;100的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">&lt;&gt;</span> <span style="color:#099">100</span> ;

</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：!=和&lt;&gt;都是判断不等于的意思，但是MySQL推荐使用&lt;&gt;</p>
</blockquote>
<h6 id="9-交集--and">9， 交集  and</h6>
<p>9、查询工资 12000&lt;工资&lt;18000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">12000</span> <span style="color:#000;font-weight:bold">AND</span> salary <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#099">18000</span> ;

<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> employeees <span style="color:#000;font-weight:bold">where</span> salary <span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">12000</span> <span style="color:#000;font-weight:bold">and</span> salary <span style="color:#000;font-weight:bold">&lt;</span><span style="color:#099">18000</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="10并集-or">10，并集 or</h6>
<p>10、查询工资&lt;=12000||工资&gt;=18000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&lt;=</span> <span style="color:#099">12000</span> <span style="color:#000;font-weight:bold">OR</span> salary <span style="color:#000;font-weight:bold">&gt;=</span> <span style="color:#099">18000</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="11补充并集-not--and">11，（补充）并集 not  and</h6>
<p>11、查询工资&lt;=12000||工资&gt;=18000的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#000;font-weight:bold">NOT</span> (salary <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">12000</span> <span style="color:#000;font-weight:bold">AND</span> salary <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#099">18000</span>) ;

</code></pre></td></tr></table>
</div>
</div><p><strong>12，模糊查询 like</strong></p>
<p>12、查询员工名中第一个字符为B、第四个字符为d的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span>
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> last_name <span style="color:#000;font-weight:bold">LIKE</span> <span style="color:#d14">&#39;B__d%&#39;</span> ;


<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> employees <span style="color:#000;font-weight:bold">where</span> last_name <span style="color:#000;font-weight:bold">like</span> <span style="color:#d14">&#39;B__d%&#39;</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="13-在之间-between--and">13， 在之间 between  and</h6>
<p>13、查询员工编号在100到120之间的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">BETWEEN</span> <span style="color:#099">100</span> <span style="color:#000;font-weight:bold">AND</span> <span style="color:#099">120</span> ;


<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> employees <span style="color:#000;font-weight:bold">where</span> employee_id <span style="color:#000;font-weight:bold">between</span> <span style="color:#099">100</span> <span style="color:#000;font-weight:bold">and</span> <span style="color:#099">120</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="14-不在-之间-not-between--and">14, 不在 之间 not between  and</h6>
<p>14、查询员工编号不在100到120之间的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">BETWEEN</span> <span style="color:#099">100</span> <span style="color:#000;font-weight:bold">AND</span> <span style="color:#099">120</span> ;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="15在集合中-in">15，在集合中 in</h6>
<p>15、查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  last_name,
  job_id 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> job_id <span style="color:#000;font-weight:bold">IN</span> (<span style="color:#d14">&#39;IT_PROT&#39;</span>, <span style="color:#d14">&#39;AD_VP&#39;</span>, <span style="color:#d14">&#39;AD_PRES&#39;</span>) ;

<span style="color:#000;font-weight:bold">select</span> ast_name, job_id  <span style="color:#000;font-weight:bold">from</span>  employees <span style="color:#000;font-weight:bold">where</span> job_id <span style="color:#000;font-weight:bold">in</span> (<span style="color:#d14">&#39;IT_PROT&#39;</span>, <span style="color:#d14">&#39;AD_VP&#39;</span>, <span style="color:#d14">&#39;AD_PRES&#39;</span>);
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：in列表的值类型必须一致或兼容，in列表中不支持通配符%和_</p>
</blockquote>
<h6 id="16-判断空-is-null">16， 判断空 is null</h6>
<p>16、查询没有奖金的员工名和奖金率</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  last_name,
  commission_pct 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> commission_pct <span style="color:#000;font-weight:bold">IS</span> <span style="color:#000;font-weight:bold">NULL</span> ;

<span style="color:#000;font-weight:bold">select</span>  last_name,commission_pct <span style="color:#000;font-weight:bold">from</span> employees <span style="color:#000;font-weight:bold">where</span> commission_pct <span style="color:#000;font-weight:bold">&lt;=&gt;</span> <span style="color:#000;font-weight:bold">NULL</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="17-判断不为空-is-not-null">17， 判断不为空 is not null</h6>
<p>17、查询有奖金的员工名和奖金率</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  last_name,
  commission_pct 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> commission_pct <span style="color:#000;font-weight:bold">IS</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> ;

</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：=、!=不能用来判断NULL、而&lt;=&gt;、is null 、 is not null可以用来判断NULL，但注意&lt;=&gt;也可以判断普通类型的数值 .</p>
</blockquote>
<h4 id="13-排序查询">1.3 ，排序查询</h4>
<h5 id="select-查询列表-from-表名-where-筛选条件-order-by-排序列表-ascdesc">select 查询列表 from 表名 [where 筛选条件] order by 排序列表 [asc|desc]</h5>
<p>order by是mysql中对查询数据进行排序的方法:</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span> <span style="color:#a61717;background-color:#e3d2d2">列名</span>(<span style="color:#a61717;background-color:#e3d2d2">或者数字</span>) <span style="color:#000;font-weight:bold">asc</span><span style="color:#a61717;background-color:#e3d2d2">；升序</span>(<span style="color:#a61717;background-color:#e3d2d2">默认升序</span>)
<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span> <span style="color:#a61717;background-color:#e3d2d2">列名</span>(<span style="color:#a61717;background-color:#e3d2d2">或者数字</span>) <span style="color:#000;font-weight:bold">desc</span><span style="color:#a61717;background-color:#e3d2d2">；降序</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>这里的重点在于order by后既可以填列名或者是一个数字</p>
</blockquote>
<p>举个例子： id是user表的第一列的列名，那么如果想根据id来排序，有两种写法</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#000;font-weight:bold">user</span> <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span> id;
selecr <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#000;font-weight:bold">user</span> <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span> <span style="color:#099">1</span>;
</code></pre></td></tr></table>
</div>
</div><p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  <span style="color:#a61717;background-color:#e3d2d2">表</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件】</span>
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">排序列表</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">asc</span> <span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">desc</span><span style="color:#a61717;background-color:#e3d2d2">】</span> ;

<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> [<span style="color:#000;font-weight:bold">where</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span>] <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span>  <span style="color:#a61717;background-color:#e3d2d2">排序列表</span> [<span style="color:#000;font-weight:bold">asc</span><span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">desc</span>];
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p><strong>二、注意</strong></p>
<ol>
<li>排序列表可以是单个字段、多个字段、别名、函数、表达式</li>
<li>asc代表升序，desc代表降序，如果不写，默认是asc</li>
<li>order by的位置一般放在查询语句的最后（除limit语句之外）</li>
</ol>
</blockquote>
<p><strong>三、示例</strong></p>
<h6 id="1查询单个字符-降序">1，查询单个字符 降序</h6>
<p>1、按单个字段排序：查询员工信息，要求按工资降序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> salary <span style="color:#000;font-weight:bold">DESC</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="2-查询多个字符">2， 查询多个字符</h6>
<p>2、按多个字段查询：查询员工信息，要求先按工资降序，再按员工编号升序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> salary <span style="color:#000;font-weight:bold">DESC</span>, employee_id <span style="color:#000;font-weight:bold">ASC</span> ;

<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">from</span> employees <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span> selary <span style="color:#000;font-weight:bold">desc</span>, employee_id <span style="color:#000;font-weight:bold">asc</span>;
</code></pre></td></tr></table>
</div>
</div><h6 id="3按照别名排序">3，按照别名排序</h6>
<p>3、按别名排序查询：查询员工信息，要求按员工年薪升序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span>,
  salary <span style="color:#000;font-weight:bold">*</span> <span style="color:#099">12</span> <span style="color:#000;font-weight:bold">*</span> (<span style="color:#099">1</span><span style="color:#000;font-weight:bold">+</span> IFNULL(commission_pct, <span style="color:#099">0</span>)) <span style="color:#000;font-weight:bold">as</span> <span style="color:#a61717;background-color:#e3d2d2">年薪</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">年薪</span> <span style="color:#000;font-weight:bold">ASC</span> ;

</code></pre></td></tr></table>
</div>
</div><h6 id="4按照函数排序">4，按照函数排序</h6>
<p>4、按函数排序查询：查询员工信息，要求按员工名字的长度降序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">LENGTH</span>(last_name),
  last_name 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#000;font-weight:bold">LENGTH</span>(last_name) <span style="color:#000;font-weight:bold">DESC</span> ;
<span style="color:#998;font-style:italic">-- LENGTH() 长度
</span></code></pre></td></tr></table>
</div>
</div><h6 id="5按照表达式排序">5，按照表达式排序</h6>
<p>5、按表达式排序：查询员工信息，要求按员工年薪升序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span>,
  salary <span style="color:#000;font-weight:bold">*</span> <span style="color:#099">12</span> <span style="color:#000;font-weight:bold">*</span> (<span style="color:#099">1</span><span style="color:#000;font-weight:bold">+</span> IFNULL(commission_pct, <span style="color:#099">0</span>)) 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> salary <span style="color:#000;font-weight:bold">*</span> <span style="color:#099">12</span> <span style="color:#000;font-weight:bold">*</span> (<span style="color:#099">1</span><span style="color:#000;font-weight:bold">+</span> IFNULL(commission_pct, <span style="color:#099">0</span>)) <span style="color:#000;font-weight:bold">ASC</span> ;
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="14单行函数">1.4、单行函数</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>(<span style="color:#a61717;background-color:#e3d2d2">实参列表</span>) <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表】</span>;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>(<span style="color:#a61717;background-color:#e3d2d2">实参列表</span>) 
</code></pre></td></tr></table>
</div>
</div><p><strong>二、分类</strong></p>
<ol>
<li>
<p>字符函数</p>
<ul>
<li>concat：连接字符</li>
<li>substr：截取子串</li>
<li>replace：替换字符</li>
<li>upper：变大写</li>
<li>lower：变小写</li>
<li>lpad：左填充</li>
<li>rpad：右填充</li>
<li>length：获取字节长度</li>
<li>trim：去除前后空格</li>
<li>instr：获取子串第一次出现的索引</li>
</ul>
<blockquote>
<p>1、注意MySQL中的索引是从1开始的</p>
</blockquote>
</li>
<li>
<p>数学函数</p>
<ul>
<li>round：四舍五入</li>
<li>ceil：向上取整</li>
<li>floor：向下取整</li>
<li>mod：取模运算（a-a/b*b）</li>
<li>truncate：保留小数的位数，不进行四舍五入</li>
<li>rand：获取随机数，返回0-1之间的小数</li>
</ul>
</li>
<li>
<p>日期函数</p>
<ul>
<li>now：返回当前日期+时间</li>
<li>curdate：返回当前日期</li>
<li>curtime：返回当前时间</li>
<li>year：返回年</li>
<li>month：返回月</li>
<li>day：返回日</li>
<li>hour：小时</li>
<li>minute：分钟</li>
<li>second：秒</li>
<li>monthname：以英文形式返回月</li>
<li>datediff：返回两个日期相差的天数</li>
<li>date_format：将日期转换成字符</li>
<li>str_to_date：将字符转换成日期</li>
</ul>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">格式符：</span>
<span style="color:#000;font-weight:bold">%</span>Y<span style="color:#a61717;background-color:#e3d2d2">：四位的年份</span>
<span style="color:#000;font-weight:bold">%</span>y<span style="color:#a61717;background-color:#e3d2d2">：二位的年份</span>
<span style="color:#000;font-weight:bold">%</span>m<span style="color:#a61717;background-color:#e3d2d2">：二位的月份（</span><span style="color:#099">01</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">02</span><span style="color:#a61717;background-color:#e3d2d2">，</span>...<span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">12</span><span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">%</span><span style="color:#000;font-weight:bold">c</span><span style="color:#a61717;background-color:#e3d2d2">：一位的月份（</span><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">，</span>...<span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">12</span><span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">%</span>d<span style="color:#a61717;background-color:#e3d2d2">：日（</span><span style="color:#099">01</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">02</span><span style="color:#a61717;background-color:#e3d2d2">，</span>...<span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">31</span><span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">%</span>H<span style="color:#a61717;background-color:#e3d2d2">：小时（</span><span style="color:#099">24</span><span style="color:#a61717;background-color:#e3d2d2">小时制）</span>
<span style="color:#000;font-weight:bold">%</span>h<span style="color:#a61717;background-color:#e3d2d2">：小时（</span><span style="color:#099">12</span><span style="color:#a61717;background-color:#e3d2d2">小时制）</span>
<span style="color:#000;font-weight:bold">%</span>i<span style="color:#a61717;background-color:#e3d2d2">：分钟（</span><span style="color:#099">00</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">01</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">02</span><span style="color:#a61717;background-color:#e3d2d2">，</span>...<span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">59</span><span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">%</span>s<span style="color:#a61717;background-color:#e3d2d2">：秒（</span><span style="color:#099">00</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">01</span><span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">02</span><span style="color:#a61717;background-color:#e3d2d2">，</span>...<span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#099">59</span><span style="color:#a61717;background-color:#e3d2d2">）</span>    
<span style="color:#099">12345678910</span>
</code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>控制函数</p>
<ul>
<li>
<p>if：判断函数</p>
<blockquote>
<p>1、IF(条件表达式，表达式1，表达式2)：如果条件表达式成立，返回表达式1，否则返回表达式2</p>
</blockquote>
</li>
<li>
<p>case：分支函数</p>
<blockquote>
<p>case的格式一：</p>
<p>case 变量或字段或表达式 
when常量1 then 值1 
when 常量2 then 值2
&hellip;
else  值n 
end ;</p>
<p>case的格式二：</p>
<p>CASE
WHEN 条件1 THEN 值1 
WHEN 条件2 THEN 值2
&hellip;
ELSE 值n 
END</p>
</blockquote>
</li>
</ul>
</li>
<li>
<p>其它函数</p>
<ul>
<li>version：当前数据库的版本</li>
<li>database：当前打开的数据库</li>
<li>user：当前登录的用户</li>
<li>password(‘字符’)：返回该字符的密码形式</li>
<li>md5(‘字符’)：返回该字符的md5加密形式</li>
</ul>
</li>
</ol>
<p><strong>三、演示</strong></p>
<h6 id="1concat-连接字符">1、concat 连接字符</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> CONCAT(<span style="color:#d14">&#39;Hello&#39;</span>,<span style="color:#d14">&#39; &#39;</span>,<span style="color:#d14">&#39;World&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="2substr-截取字符">2、substr 截取字符</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">截取从指定索引处后面所有字符</span>
<span style="color:#000;font-weight:bold">SELECT</span> SUBSTR(<span style="color:#d14">&#39;李莫愁爱上了陆展元&#39;</span>,<span style="color:#099">7</span>)  <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">陆展元</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">截取从指定索引处指定字符长度的字符</span>
<span style="color:#000;font-weight:bold">SELECT</span> SUBSTR(<span style="color:#d14">&#39;李莫愁爱上了陆展元&#39;</span>,<span style="color:#099">1</span>,<span style="color:#099">3</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">李莫愁</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="3replace-替换字符">3、replace :替换字符</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">REPLACE</span>(<span style="color:#d14">&#39;张无忌爱上了周芷若&#39;</span>,<span style="color:#d14">&#39;周芷若&#39;</span>,<span style="color:#d14">&#39;赵敏&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">张无忌爱上了赵敏</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="4uppper---变大写">4、uppper :  变大写</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">UPPER</span>(<span style="color:#d14">&#39;john&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
JOHN
</code></pre></td></tr></table>
</div>
</div><h6 id="5lower-变小写">5、lower 变小写</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">LOWER</span>(<span style="color:#d14">&#39;JOHN&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
john
</code></pre></td></tr></table>
</div>
</div><h6 id="6lpad-左填充">6、lpad ：左填充</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> LPAD(<span style="color:#d14">&#39;殷素素&#39;</span>,<span style="color:#099">10</span>,<span style="color:#d14">&#39;*&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#000;font-weight:bold">*******</span><span style="color:#a61717;background-color:#e3d2d2">殷素素</span>

</code></pre></td></tr></table>
</div>
</div><h6 id="7rpad-右填充">7、rpad: 右填充</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> RPAD(<span style="color:#d14">&#39;殷&#39;</span>,<span style="color:#099">10</span>,<span style="color:#d14">&#39;*&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">殷</span><span style="color:#000;font-weight:bold">*********</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="8length-获取字符长度">8、length： 获取字符长度</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">LENGTH</span>(<span style="color:#d14">&#39;john&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">length</span>(<span style="color:#d14">&#39;john&#39;</span>) <span style="color:#000;font-weight:bold">as</span> out_put;
<span style="color:#099">4</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="9trim去除前后空格或指定字符">9、trim：去除前后空格或指定字符</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除指定字符的左右空格</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">LENGTH</span>(<span style="color:#000;font-weight:bold">TRIM</span>(<span style="color:#d14">&#39;    张翠山    &#39;</span>)) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">9</span> <span style="color:#a61717;background-color:#e3d2d2">（一个汉字三个字符）</span>

<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">trim</span>(<span style="color:#d14">&#39;        张翠山       &#39;</span>)<span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">张翠山</span>

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除指定字符的指定字符</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">TRIM</span>(<span style="color:#d14">&#39;a&#39;</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#d14">&#39;aaaaaaaaa张翠山aaaaaaaaa&#39;</span>)  <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">张翠山</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="10instr-获取子串第一次出现的索引">10、instr: 获取子串第一次出现的索引</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> INSTR(<span style="color:#d14">&#39;杨不悔爱上了殷六侠&#39;</span>,<span style="color:#d14">&#39;殷六侠&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">7</span>

</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：返回子串第一次出现的索引，如果找不到返回0</p>
</blockquote>
<h6 id="11-round四舍五入">11、 round：四舍五入</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">默认四舍五入</span>
<span style="color:#000;font-weight:bold">SELECT</span> ROUND(<span style="color:#000;font-weight:bold">-</span><span style="color:#099">1</span>.<span style="color:#099">55</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#000;font-weight:bold">-</span><span style="color:#099">2</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">指定小数位数</span>
<span style="color:#000;font-weight:bold">SELECT</span> ROUND(<span style="color:#099">1</span>.<span style="color:#099">567</span>,<span style="color:#099">2</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
  <span style="color:#099">1</span>.<span style="color:#099">57</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：四舍五入和符号无关</p>
</blockquote>
<h6 id="12ceil向上取整">12、ceil：向上取整</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> CEIL(<span style="color:#000;font-weight:bold">-</span><span style="color:#099">1</span>.<span style="color:#099">02</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：向上取整，返回&gt;=该参数的最小整数</p>
</blockquote>
<h6 id="13floor向下取整">13、floor：向下取整</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> FLOOR(<span style="color:#000;font-weight:bold">-</span><span style="color:#099">9</span>.<span style="color:#099">99</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#000;font-weight:bold">-</span><span style="color:#099">10</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注意：向下取整，返回&lt;=该参数的最大整数</p>
</blockquote>
<h6 id="14mod取模运算a-abb">14、mod：取模运算（a-a/b*b）</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MOD</span>(<span style="color:#099">10</span>,<span style="color:#099">3</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="15truncate保留小数的位数不进行四舍五入">15、truncate：保留小数的位数，不进行四舍五入</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">TRUNCATE</span>(<span style="color:#099">1</span>.<span style="color:#099">69999</span>,<span style="color:#099">1</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
  <span style="color:#099">1</span>.<span style="color:#099">6</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="16rand获取随机数返回0-1之间的小数">16、rand：获取随机数，返回0-1之间的小数</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> RAND() <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">0</span>.<span style="color:#099">38037824386766017</span>

<span style="color:#a61717;background-color:#e3d2d2">取</span><span style="color:#099">10</span><span style="color:#a61717;background-color:#e3d2d2">以内的随机数</span>
<span style="color:#000;font-weight:bold">select</span>  <span style="color:#000;font-weight:bold">truncate</span>(rand(),<span style="color:#099">1</span>) <span style="color:#000;font-weight:bold">*</span><span style="color:#099">10</span>;
<span style="color:#099">4</span>.<span style="color:#099">0</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="17now返回当前日期时间">17、now：返回当前日期+时间</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> NOW() <span style="color:#000;font-weight:bold">AS</span> out_put;
 <span style="color:#099">2020</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">12</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">25</span> <span style="color:#099">11</span>:<span style="color:#099">38</span>:<span style="color:#099">21</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="18curdate返回当前日期">18、curdate：返回当前日期</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> CURDATE() <span style="color:#000;font-weight:bold">AS</span> out_put;
 <span style="color:#099">2020</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">12</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">25</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="19curtime返回当前时间">19、curtime：返回当前时间</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> CURTIME() <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">11</span>:<span style="color:#099">40</span>:<span style="color:#099">25</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="20year返回年">20、year：返回年</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">YEAR</span>(NOW()) <span style="color:#a61717;background-color:#e3d2d2">年</span>;
<span style="color:#099">2020</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="21month返回月">21、month：返回月</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MONTH</span>(NOW()) <span style="color:#a61717;background-color:#e3d2d2">月</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="22day返回日">22、day：返回日</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">DAY</span>(NOW()) <span style="color:#a61717;background-color:#e3d2d2">日</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="23hour小时">23、hour：小时</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> HOUR(NOW()) <span style="color:#a61717;background-color:#e3d2d2">时</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="24-minute分钟">24、 minute：分钟</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MINUTE</span>(NOW()) <span style="color:#a61717;background-color:#e3d2d2">分</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="25second秒">25、second：秒</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">SECOND</span>(NOW()) <span style="color:#a61717;background-color:#e3d2d2">秒</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="26monthname以英文形式返回月">26、monthname：以英文形式返回月</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> MONTHNAME(NOW()) <span style="color:#a61717;background-color:#e3d2d2">月名</span>;
December <span style="color:#099">12</span><span style="color:#a61717;background-color:#e3d2d2">月</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="27datediff返回两个日期相差的天数">27、datediff：返回两个日期相差的天数</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> DATEDIFF(<span style="color:#d14">&#39;1995-2-7&#39;</span>,<span style="color:#d14">&#39;1995-2-6&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">1</span>

 <span style="color:#000;font-weight:bold">SELECT</span> DATEDIFF(curdate(),<span style="color:#d14">&#39;1998-08-18&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
   <span style="color:#099">8165</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="28date_format将日期转换成字符">28、date_format：将日期转换成字符</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> DATE_FORMAT(NOW(),<span style="color:#d14">&#39;%Y年%m月%d日&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">2020</span><span style="color:#a61717;background-color:#e3d2d2">年</span><span style="color:#099">12</span><span style="color:#a61717;background-color:#e3d2d2">月</span><span style="color:#099">25</span><span style="color:#a61717;background-color:#e3d2d2">日</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="29str_to_date将字符转换成日期">29、str_to_date：将字符转换成日期</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> STR_TO_DATE(<span style="color:#d14">&#39;1998-3-2&#39;</span>,<span style="color:#d14">&#39;%Y-%c-%d&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#099">1998</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">03</span><span style="color:#000;font-weight:bold">-</span><span style="color:#099">02</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="30if-条件">30、if 条件</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">IF</span>(<span style="color:#099">10</span> <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#099">5</span>, <span style="color:#d14">&#39;大&#39;</span>, <span style="color:#d14">&#39;小&#39;</span>) <span style="color:#000;font-weight:bold">AS</span> out_put;
<span style="color:#a61717;background-color:#e3d2d2">小</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="31case的格式一">31、case的格式一</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">30
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">31
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">32
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">33
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">34
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">35
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#998;font-style:italic">/*
</span><span style="color:#998;font-style:italic">案例：查询员工的工资，要求
</span><span style="color:#998;font-style:italic">	部门号=30，显示的工资为1.1倍
</span><span style="color:#998;font-style:italic">	部门号=40，显示的工资为1.2倍
</span><span style="color:#998;font-style:italic">	部门号=50，显示的工资为1.3倍
</span><span style="color:#998;font-style:italic">	其它部门，显示的工资为原工资
</span><span style="color:#998;font-style:italic">*/</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  salary <span style="color:#a61717;background-color:#e3d2d2">原始工资</span>,
  department_id,
  <span style="color:#000;font-weight:bold">CASE</span>
    department_id 
    <span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#099">30</span> <span style="color:#000;font-weight:bold">THEN</span> salary <span style="color:#000;font-weight:bold">*</span> <span style="color:#099">1</span>.<span style="color:#099">1</span> 
    <span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#099">40</span> <span style="color:#000;font-weight:bold">THEN</span> salary <span style="color:#000;font-weight:bold">*</span> <span style="color:#099">1</span>.<span style="color:#099">2</span> 
    <span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#099">50</span> <span style="color:#000;font-weight:bold">THEN</span> salary <span style="color:#000;font-weight:bold">*</span> <span style="color:#099">1</span>.<span style="color:#099">3</span> 
    <span style="color:#000;font-weight:bold">ELSE</span> salary 
  <span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">AS</span> <span style="color:#a61717;background-color:#e3d2d2">新工资</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees ;


<span style="color:#000;font-weight:bold">select</span>
  salary <span style="color:#a61717;background-color:#e3d2d2">原始工资</span>,
  department_id <span style="color:#a61717;background-color:#e3d2d2">部门</span>id,
  <span style="color:#000;font-weight:bold">case</span>
  	department_id
  	<span style="color:#000;font-weight:bold">when</span> <span style="color:#099">30</span> <span style="color:#000;font-weight:bold">then</span> salary<span style="color:#000;font-weight:bold">*</span><span style="color:#099">1</span>.<span style="color:#099">1</span>
  	<span style="color:#000;font-weight:bold">when</span> <span style="color:#099">40</span> <span style="color:#000;font-weight:bold">then</span> salary<span style="color:#000;font-weight:bold">*</span><span style="color:#099">1</span>.<span style="color:#099">2</span>
  	<span style="color:#000;font-weight:bold">when</span> <span style="color:#099">50</span> <span style="color:#000;font-weight:bold">then</span> salary<span style="color:#000;font-weight:bold">*</span><span style="color:#099">1</span>.<span style="color:#099">3</span>
  	<span style="color:#000;font-weight:bold">else</span> salary
  <span style="color:#000;font-weight:bold">end</span> <span style="color:#000;font-weight:bold">as</span> <span style="color:#a61717;background-color:#e3d2d2">新工资</span> 
 <span style="color:#000;font-weight:bold">from</span>
    employees ; 

</code></pre></td></tr></table>
</div>
</div><p>32、case的格式二</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#998;font-style:italic">/*
</span><span style="color:#998;font-style:italic">案例：查询员工的工资情况
</span><span style="color:#998;font-style:italic">    如果工资&gt;20000，显示A级别
</span><span style="color:#998;font-style:italic">    如果工资&gt;15000，显示B级别
</span><span style="color:#998;font-style:italic">    如果工资&gt;10000，显示C级别
</span><span style="color:#998;font-style:italic">    否则，显示D级别
</span><span style="color:#998;font-style:italic">*/</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  salary,
  <span style="color:#000;font-weight:bold">CASE</span>
    <span style="color:#000;font-weight:bold">WHEN</span> salary <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">20000</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#d14">&#39;A&#39;</span> 
    <span style="color:#000;font-weight:bold">WHEN</span> salary <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">15000</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#d14">&#39;B&#39;</span> 
    <span style="color:#000;font-weight:bold">WHEN</span> salary <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">10000</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#d14">&#39;C&#39;</span> 
    <span style="color:#000;font-weight:bold">ELSE</span> <span style="color:#d14">&#39;D&#39;</span> 
  <span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">AS</span> <span style="color:#a61717;background-color:#e3d2d2">工资级别</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees ;

</code></pre></td></tr></table>
</div>
</div><h6 id="33version当前数据库的版本">33、version：当前数据库的版本</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">VERSION</span>();
 <span style="color:#099">5</span>.<span style="color:#099">7</span>.<span style="color:#099">32</span><span style="color:#000;font-weight:bold">-</span>log
</code></pre></td></tr></table>
</div>
</div><h6 id="34-database当前打开的数据库">34、 database：当前打开的数据库</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">database</span>();
</code></pre></td></tr></table>
</div>
</div><h6 id="35-user当前登录的用户">35、 user：当前登录的用户</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">USER</span>();

<span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">user</span>();
 root<span style="color:#000;font-weight:bold">@</span>localhost
</code></pre></td></tr></table>
</div>
</div><h6 id="36-password字符返-回该字符的密码形式">36、 password(‘字符’)：返 回该字符的密码形式</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> PASSWORD(<span style="color:#d14">&#39;123456&#39;</span>);
<span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">*</span><span style="color:#099">6</span>BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
</code></pre></td></tr></table>
</div>
</div><h6 id="37--md5字符返回该字符的md5加密形式">37、  md5(‘字符’)：返回该字符的md5加密形式</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> MD5(<span style="color:#d14">&#39;123456&#39;</span>);
e10adc3949ba59abbe56e057f20f883e
</code></pre></td></tr></table>
</div>
</div><h3 id="35分组函数">3.5、分组函数</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>(<span style="color:#a61717;background-color:#e3d2d2">实参列表</span>) <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表】</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、分类</strong></p>
<ol>
<li>sum：求和</li>
<li>avg：平均值</li>
<li>max：最大值</li>
<li>min：最小值</li>
<li>count：计算个数</li>
</ol>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、</span><span style="color:#000;font-weight:bold">sum</span><span style="color:#a61717;background-color:#e3d2d2">、</span>avg一般用于处理数值型<span style="color:#a61717;background-color:#e3d2d2">，</span><span style="color:#000;font-weight:bold">max</span><span style="color:#a61717;background-color:#e3d2d2">、</span><span style="color:#000;font-weight:bold">min</span><span style="color:#a61717;background-color:#e3d2d2">、</span>count可以处理任何类型

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、以上分组函数都忽略</span>null值

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、可以和</span>distinct搭配实现去重的运算<span style="color:#a61717;background-color:#e3d2d2">：</span><span style="color:#000;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">sum</span>(<span style="color:#000;font-weight:bold">distinct</span> <span style="color:#a61717;background-color:#e3d2d2">字段</span>) <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表</span>;

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、一般使用</span><span style="color:#000;font-weight:bold">count</span>(<span style="color:#000;font-weight:bold">*</span>)<span style="color:#a61717;background-color:#e3d2d2">用作统计行数</span>vb

<span style="color:#099">5</span><span style="color:#a61717;background-color:#e3d2d2">、和分组函数一同查询的字段要求是</span><span style="color:#000;font-weight:bold">group</span> by后的字段
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>三、演示</strong></p>
<h6 id="1sum求和">1、sum：求和</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">SUM</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="2sum求和">2、sum：求和</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">AVG</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="3max最大值">3、max：最大值</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MAX</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="4min最小值">4、min：最小值</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MIN</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h6 id="5-count计算个数">5、 count：计算个数</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">COUNT</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="36分组查询-grop-by">3.6、分组查询 grop by</h3>
<h5 id="select-查询列表-from-表名-where-筛选条件-group-by-分组的字段--having-分组后的筛选--order-by-排序的字段-">select 查询列表 from 表名 where 筛选条件 group by 分组的字段  having 分组后的筛选  order by 排序的字段 ;</h5>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  <span style="color:#a61717;background-color:#e3d2d2">表</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">where</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件】</span> 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">分组的字段</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">having</span> <span style="color:#a61717;background-color:#e3d2d2">分组后的筛选】</span>
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">排序的字段】</span> ;

<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">where</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span> <span style="color:#000;font-weight:bold">group</span> <span style="color:#000;font-weight:bold">by</span> <span style="color:#a61717;background-color:#e3d2d2">分组的字段</span>  <span style="color:#000;font-weight:bold">having</span> <span style="color:#a61717;background-color:#e3d2d2">分组后的筛选</span>  <span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">by</span> <span style="color:#a61717;background-color:#e3d2d2">排序的字段</span> ;
</code></pre></td></tr></table>
</div>
</div><p><strong>二、特点</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、和分组函数一同查询的字段必须是</span><span style="color:#000;font-weight:bold">group</span> by后出现的字段

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、筛选分为两类：分组前筛选和分组后筛选</span>
            	<span style="color:#a61717;background-color:#e3d2d2">针对的表</span>				 <span style="color:#a61717;background-color:#e3d2d2">语句位置</span>			   <span style="color:#a61717;background-color:#e3d2d2">连接的关键字</span>
	<span style="color:#a61717;background-color:#e3d2d2">分组前筛选</span>	 <span style="color:#a61717;background-color:#e3d2d2">分组前的原始表</span>			<span style="color:#000;font-weight:bold">group</span> by前			<span style="color:#000;font-weight:bold">where</span>
	<span style="color:#a61717;background-color:#e3d2d2">分组后筛选</span>	 <span style="color:#a61717;background-color:#e3d2d2">分组后的结果集</span>			<span style="color:#000;font-weight:bold">group</span> by后			<span style="color:#000;font-weight:bold">having</span>
	
<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、分组可以按单个字段也可以按多个字段</span>

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、分组可以搭配着排序使用</span>
<span style="color:#099">12345678910</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>三、演示</strong></p>
<p>1、查询每个工种的员工平均工资</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">AVG</span>(salary),
  job_id 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> job_id ;
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><p>2、查询每个位置的员工部门个数</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">COUNT</span>(<span style="color:#000;font-weight:bold">*</span>),
  location_id 
<span style="color:#000;font-weight:bold">FROM</span>
  departments 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> location_id ;
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><p>3、查询有奖金的每个领导手下员工的平均工资</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">AVG</span>(salary),
  manager_id 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> commission_pct <span style="color:#000;font-weight:bold">IS</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> manager_id ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查询哪个部门的员工个数&gt;5</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">COUNT</span>(<span style="color:#000;font-weight:bold">*</span>),
  department_id 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> department_id 
<span style="color:#000;font-weight:bold">HAVING</span> <span style="color:#000;font-weight:bold">COUNT</span>(<span style="color:#000;font-weight:bold">*</span>) <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">5</span> ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><p>5、查询每个工种有奖金的员工的最高工资&gt;6000的最高工资和公众编号，按最高工资升序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">MAX</span>(salary) m,
  job_id
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> commission_pct <span style="color:#000;font-weight:bold">IS</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> job_id 
<span style="color:#000;font-weight:bold">HAVING</span> m <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#099">6000</span> 
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> m ;
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p>6、查询每个工种每个部门的最低工资并按最低工资降序</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">MIN</span>(salary),
  job_id,
  department_id 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> job_id, department_id
<span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#000;font-weight:bold">MIN</span>(salary) <span style="color:#000;font-weight:bold">DESC</span> ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="37连接查询">3.7、连接查询</h3>
<p><strong>一、含义</strong></p>
<p>连接查询又称多表查询，当查询的字段来自于多个表时，就会用到连接查询</p>
<p><strong>二、注意</strong></p>
<pre><code>笛卡尔乘积现象：表1 有m行，表2有n行，结果=m*n行

发生原因：没有有效的连接条件
如何避免：添加有效的连接条件

</code></pre><p><strong>三、分类</strong></p>
<ol>
<li>按年代分类
<ul>
<li>sql92标准：支持内连接</li>
<li>sql99标准：支持内连接、部分外连接（左外、右外）、交叉连接</li>
</ul>
</li>
<li>按功能分类
<ul>
<li>内连接
<ul>
<li>等值连接</li>
<li>非等值连接</li>
<li>自连接</li>
</ul>
</li>
<li>外连接
<ul>
<li>左外连接</li>
<li>右外连接</li>
<li>全外连接</li>
</ul>
</li>
<li>交叉连接</li>
</ul>
</li>
</ol>
<p><strong>四、sql92标准演示</strong></p>
<h5 id="1sql92标准等值连接">1、sql92标准：等值连接</h5>
<h6 id="select-表1字段名表2字段名-from-表1表2-where-表1相关id--表2相关id">select 表1.字段名，表2.字段名 from 表1，表2 where 表1.相关id = 表2.相关id;</h6>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询员工名和对应的部门名</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  last_name,
  department_name 
<span style="color:#000;font-weight:bold">FROM</span>
  employees,
  departments 
<span style="color:#000;font-weight:bold">WHERE</span> employees.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> departments.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> ;

<span style="color:#000;font-weight:bold">select</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span>.<span style="color:#a61717;background-color:#e3d2d2">字段名，表</span><span style="color:#099">2</span>.<span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#000;font-weight:bold">from</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">，表</span><span style="color:#099">2</span> <span style="color:#000;font-weight:bold">where</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span>.<span style="color:#a61717;background-color:#e3d2d2">相关</span>id <span style="color:#000;font-weight:bold">=</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span>.<span style="color:#a61717;background-color:#e3d2d2">相关</span>id;

</code></pre></td></tr></table>
</div>
</div><h5 id="2sql92标准非等值连接">2、sql92标准：非等值连接</h5>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询员工的工资和工资级别</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  salary,
  grade_level 
<span style="color:#000;font-weight:bold">FROM</span>
  employees e,
  job_grades <span style="color:#000;font-weight:bold">g</span> 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">BETWEEN</span> <span style="color:#000;font-weight:bold">g</span>.<span style="color:#000;font-weight:bold">`</span>lowest_sal<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">AND</span> <span style="color:#000;font-weight:bold">g</span>.<span style="color:#000;font-weight:bold">`</span>highest_sal<span style="color:#000;font-weight:bold">`</span> ;

</code></pre></td></tr></table>
</div>
</div><p>3、sql92标准：自连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询员工名和它对应上级的名称</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  e.employee_id,
  e.last_name,
  m.employee_id,
  m.last_name 
<span style="color:#000;font-weight:bold">FROM</span>
  employees e,
  employees m 
<span style="color:#000;font-weight:bold">WHERE</span> e.<span style="color:#000;font-weight:bold">`</span>manager_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> m.<span style="color:#000;font-weight:bold">`</span>employee_id<span style="color:#000;font-weight:bold">`</span> ;
<span style="color:#099">1234567891011</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="38sql99标准">3.8、sql99标准</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> 
<span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span><span style="color:#099">1</span> 
<span style="color:#a61717;background-color:#e3d2d2">【连接类型】</span> <span style="color:#000;font-weight:bold">JOIN</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span><span style="color:#099">2</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">连接条件</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">where</span> <span style="color:#a61717;background-color:#e3d2d2">分组前筛选条件】</span>
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">group</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">分组列表】</span>
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">having</span> <span style="color:#a61717;background-color:#e3d2d2">分组后筛选条件】</span>
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">order</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">排序列表】</span> ;

</code></pre></td></tr></table>
</div>
</div><p><strong>二、连接类型</strong></p>
<ol>
<li>内连接：inner</li>
<li>外连接
<ul>
<li>左外连接：left 【outer】（左边的是主表）</li>
<li>右外连接：right 【outer】（右边的是主表）</li>
<li>全外连接：full 【outer】（两边都是主表，但是MySQL不支持全外连接、Oracle支持）</li>
</ul>
</li>
<li>交叉连接：cross（交叉连接其实是用sql99语法实现笛卡尔乘积）</li>
</ol>
<p><strong>三、演示</strong></p>
<p>1、内连接：等值连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询员工名和对应的部门名</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  last_name,
  department_name 
<span style="color:#000;font-weight:bold">FROM</span>
  departments d 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">JOIN</span> employees e <span style="color:#000;font-weight:bold">ON</span> e.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> d.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><p>2、内连接：非等值连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询员工的工资和工资级别</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  salary,
  grade_level 
<span style="color:#000;font-weight:bold">FROM</span>
  employees e 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">JOIN</span> job_grades <span style="color:#000;font-weight:bold">g</span> <span style="color:#000;font-weight:bold">ON</span> e.<span style="color:#000;font-weight:bold">`</span>salary<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">BETWEEN</span> <span style="color:#000;font-weight:bold">g</span>.<span style="color:#000;font-weight:bold">`</span>lowest_sal<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">AND</span> <span style="color:#000;font-weight:bold">g</span>.<span style="color:#000;font-weight:bold">`</span>highest_sal<span style="color:#000;font-weight:bold">`</span> ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><p>3、内连接：自连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询员工名和它对应上级的名称</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  e.last_name,
  m.last_name 
<span style="color:#000;font-weight:bold">FROM</span>
  employees e 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">JOIN</span> employees m <span style="color:#000;font-weight:bold">ON</span> e.<span style="color:#000;font-weight:bold">`</span>manager_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> m.<span style="color:#000;font-weight:bold">`</span>employee_id<span style="color:#000;font-weight:bold">`</span> ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><p>4、外连接：左外连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询哪个部门没有员工</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  d.<span style="color:#000;font-weight:bold">`</span>department_name<span style="color:#000;font-weight:bold">`</span>
<span style="color:#000;font-weight:bold">FROM</span>
  departments d 
<span style="color:#000;font-weight:bold">LEFT</span> <span style="color:#000;font-weight:bold">OUTER</span> <span style="color:#000;font-weight:bold">JOIN</span> employees e <span style="color:#000;font-weight:bold">ON</span> d.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> e.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> 
<span style="color:#000;font-weight:bold">WHERE</span> e.<span style="color:#000;font-weight:bold">`</span>employee_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">IS</span> <span style="color:#000;font-weight:bold">NULL</span> ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><p>5、外连接：右外连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询哪个部门没有员工</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  d.<span style="color:#000;font-weight:bold">`</span>department_name<span style="color:#000;font-weight:bold">`</span>
<span style="color:#000;font-weight:bold">FROM</span>
  employees e 
<span style="color:#000;font-weight:bold">RIGHT</span> <span style="color:#000;font-weight:bold">OUTER</span> <span style="color:#000;font-weight:bold">JOIN</span> departments d <span style="color:#000;font-weight:bold">ON</span> d.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> e.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> 
<span style="color:#000;font-weight:bold">WHERE</span> e.<span style="color:#000;font-weight:bold">`</span>employee_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">IS</span> <span style="color:#000;font-weight:bold">NULL</span> ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><p>6、交叉连接</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">使用交叉连接进行笛卡尔乘积查询</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  b.<span style="color:#000;font-weight:bold">*</span>,
  bo.<span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span> beauty b 
<span style="color:#000;font-weight:bold">CROSS</span> <span style="color:#000;font-weight:bold">JOIN</span> boys bo ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="39子查询">3.9、子查询</h3>
<p><strong>一、含义</strong></p>
<p>嵌套在其它语句内部的select语句称为子查询或内查询，外面的语句可以是insert、delete、update、select等，一般select作为外面语句较多，外面如果为select语句，则此语句称为外查询或主查询</p>
<p><strong>二、分类</strong></p>
<ol>
<li>按出现的位置划分
<ul>
<li>select后面：标量子查询</li>
<li>from后面：表子查询</li>
<li>where或having后面
<ul>
<li>标量子查询</li>
<li>列子查询</li>
<li>行子查询</li>
</ul>
</li>
<li>exists后面
<ul>
<li>标量子查询</li>
<li>列子查询</li>
<li>行子查询</li>
<li>表子查询</li>
</ul>
</li>
</ul>
</li>
<li>按结果集行列数划分
<ul>
<li>标量子查询（单行子查询）：结果集为一行一列</li>
<li>列子查询（多行子查询）：结果集为多行一列</li>
<li>行子查询：结果集为多行多列</li>
<li>表子查询：结果集为多行多列</li>
</ul>
</li>
</ol>
<p><strong>三、特点</strong></p>
<ol>
<li>子查询放在小括号内</li>
<li>子查询一般放在条件的右侧</li>
<li>子查询的执行优先于主查询执行，主查询的条件用到了子查询的结果</li>
<li>标量子查询，一般搭配着单行操作符使用：&gt;、&gt;=、&lt;、&lt;=、!=、&lt;&gt;、=、&lt;=&gt;</li>
<li>列子查询，一般搭配着多行操作符使用：in、not in、any、some、all、exits</li>
</ol>
<p><strong>四、演示</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询每个部门的员工个数</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  d.<span style="color:#000;font-weight:bold">*</span>, (
  <span style="color:#000;font-weight:bold">SELECT</span> 
    <span style="color:#000;font-weight:bold">COUNT</span>(<span style="color:#000;font-weight:bold">*</span>) 
  <span style="color:#000;font-weight:bold">FROM</span>
    employees e 
  <span style="color:#000;font-weight:bold">WHERE</span> e.department_id <span style="color:#000;font-weight:bold">=</span> d.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span>
) <span style="color:#a61717;background-color:#e3d2d2">个数</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  departments d ;
<span style="color:#099">123456789101112</span>
</code></pre></td></tr></table>
</div>
</div><p>2、from后面</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询每个部门平均工资的工资等级</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  ag_dep.<span style="color:#000;font-weight:bold">*</span>,
  <span style="color:#000;font-weight:bold">g</span>.<span style="color:#000;font-weight:bold">`</span>grade_level<span style="color:#000;font-weight:bold">`</span> 
<span style="color:#000;font-weight:bold">FROM</span> (
  <span style="color:#000;font-weight:bold">SELECT</span> 
    <span style="color:#000;font-weight:bold">AVG</span>(salary) ag,
    department_id 
  <span style="color:#000;font-weight:bold">FROM</span>
    employees 
  <span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> department_id
) ag_dep 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">JOIN</span> job_grades <span style="color:#000;font-weight:bold">g</span> <span style="color:#000;font-weight:bold">ON</span> ag_dep.ag <span style="color:#000;font-weight:bold">BETWEEN</span> lowest_sal <span style="color:#000;font-weight:bold">AND</span> highest_sal ;
<span style="color:#099">1234567891011121314</span>
</code></pre></td></tr></table>
</div>
</div><p>3、where或having后面</p>
<p>标量子查询：查询最低工资的员工姓名和工资</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">9
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  last_name,
  salary 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">=</span> (
	<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MIN</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees
) ;
<span style="color:#099">12345678</span>
</code></pre></td></tr></table>
</div>
</div><p>列子查询：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">30
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">31
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">32
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">33
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">34
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">35
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">36
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询所有是领导的员工姓名</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  last_name 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> employee_id <span style="color:#000;font-weight:bold">IN</span> (
	<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">DISTINCT</span> manager_id <span style="color:#000;font-weight:bold">FROM</span> employees
) ;
<span style="color:#099">123456789</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">返回其它工种中比</span>job_id为<span style="color:#a61717;background-color:#e3d2d2">‘</span>IT_PROG<span style="color:#a61717;background-color:#e3d2d2">’工种任一工资低的员工的员工号、姓名、</span>job_id以及salary

<span style="color:#000;font-weight:bold">SELECT</span> 
  employee_id,
  last_name,
  job_id,
  salary 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#000;font-weight:bold">ANY</span> (
  <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">DISTINCT</span> salary <span style="color:#000;font-weight:bold">FROM</span> employees <span style="color:#000;font-weight:bold">WHERE</span> job_id <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;IT_PROG&#39;</span>
) <span style="color:#000;font-weight:bold">AND</span> job_id <span style="color:#000;font-weight:bold">&lt;&gt;</span> <span style="color:#d14">&#39;IT_PROG&#39;</span> ;
<span style="color:#099">123456789101112</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">返回其它部门中比</span>job_id为<span style="color:#a61717;background-color:#e3d2d2">‘</span>IT_PROG<span style="color:#a61717;background-color:#e3d2d2">’部门所有工资都低的员工的员工号、姓名、</span>job_id以及salary

<span style="color:#000;font-weight:bold">SELECT</span> 
  employee_id,
  last_name,
  job_id,
  salary 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> salary <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#000;font-weight:bold">ALL</span> (
  <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">DISTINCT</span> salary <span style="color:#000;font-weight:bold">FROM</span> employees <span style="color:#000;font-weight:bold">WHERE</span> job_id <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;IT_PROG&#39;</span>
) <span style="color:#000;font-weight:bold">AND</span> job_id <span style="color:#000;font-weight:bold">&lt;&gt;</span> <span style="color:#d14">&#39;IT_PROG&#39;</span> ;
<span style="color:#099">123456789101112</span>
</code></pre></td></tr></table>
</div>
</div><p>行子查询：查询员工编号最小并且工资最高的员工信息</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#000;font-weight:bold">*</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  employees 
<span style="color:#000;font-weight:bold">WHERE</span> (employee_id, salary) <span style="color:#000;font-weight:bold">=</span> (
	<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">MIN</span>(employee_id), <span style="color:#000;font-weight:bold">MAX</span>(salary) <span style="color:#000;font-weight:bold">FROM</span> employees
) ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><p>4、exists后面</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询有员工的部门名</span>

<span style="color:#000;font-weight:bold">SELECT</span> 
  department_name 
<span style="color:#000;font-weight:bold">FROM</span>
  departments d 
<span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#000;font-weight:bold">EXISTS</span> (
	<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">FROM</span> employees e
	<span style="color:#000;font-weight:bold">WHERE</span> e.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> d.<span style="color:#000;font-weight:bold">`</span>department_id<span style="color:#000;font-weight:bold">`</span>
) ;
<span style="color:#099">12345678910</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="310分页查询">3.10、分页查询</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> 
  <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span><span style="color:#099">1</span>
<span style="color:#a61717;background-color:#e3d2d2">【连接类型】</span> <span style="color:#000;font-weight:bold">JOIN</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span><span style="color:#099">2</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">连接条件</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">分组前的筛选】</span>
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">GROUP</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">分组字段】</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">HAVING</span> <span style="color:#a61717;background-color:#e3d2d2">分组后的筛选</span> <span style="color:#a61717;background-color:#e3d2d2">】</span>
<span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">ORDER</span> <span style="color:#000;font-weight:bold">BY</span> <span style="color:#a61717;background-color:#e3d2d2">排序字段</span> <span style="color:#000;font-weight:bold">ASC</span><span style="color:#000;font-weight:bold">|</span><span style="color:#000;font-weight:bold">DESC</span><span style="color:#a61717;background-color:#e3d2d2">】</span>
<span style="color:#000;font-weight:bold">LIMIT</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">offset</span>, <span style="color:#a61717;background-color:#e3d2d2">】</span><span style="color:#000;font-weight:bold">size</span> ;
<span style="color:#099">12345678910</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、特点</strong></p>
<ol>
<li>limit语句放在查询语句的最后</li>
<li>offset代表起始索引，起始索引从0开始，size代表条目个数</li>
<li>分页语句：select 查询列表 from 表 limit (page-1)*size,size;</li>
</ol>
<p><strong>三、演示</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询前五条员工信息</span>

<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">FROM</span>  employees <span style="color:#000;font-weight:bold">LIMIT</span> <span style="color:#099">0</span>,<span style="color:#099">5</span>;
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="311联合查询">3.11、联合查询</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">查询语句</span><span style="color:#099">1</span>
<span style="color:#000;font-weight:bold">union</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">all</span><span style="color:#a61717;background-color:#e3d2d2">】</span>
<span style="color:#a61717;background-color:#e3d2d2">查询语句</span><span style="color:#099">2</span>
<span style="color:#000;font-weight:bold">union</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">all</span><span style="color:#a61717;background-color:#e3d2d2">】</span>
...
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、特点</strong></p>
<ol>
<li>要查询的结果来自于多个表且多个表没有直接的连接关系，但查询的信息一致时，可以使用联合查询</li>
<li>要求多条查询语句的查询列数是一致的</li>
<li>要求多条查询语句的查询的每一列的类型和顺序最好一致</li>
<li>union关键字默认去重，如果使用union all可以包含重复项</li>
</ol>
<p><strong>三、演示</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">查询中国用户中男性的信息以及外国用户中年男性的用户信息</span>

<span style="color:#000;font-weight:bold">SELECT</span> id,cname <span style="color:#000;font-weight:bold">FROM</span> t_ca <span style="color:#000;font-weight:bold">WHERE</span> csex<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;男&#39;</span>
<span style="color:#000;font-weight:bold">UNION</span> <span style="color:#000;font-weight:bold">ALL</span>
<span style="color:#000;font-weight:bold">SELECT</span> t_id,tname <span style="color:#000;font-weight:bold">FROM</span> t_ua <span style="color:#000;font-weight:bold">WHERE</span> tGender<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;male&#39;</span>;

<span style="color:#000;font-weight:bold">select</span> id,cname <span style="color:#000;font-weight:bold">from</span> t_ca <span style="color:#000;font-weight:bold">where</span> csex<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;男&#39;</span> <span style="color:#000;font-weight:bold">union</span> <span style="color:#000;font-weight:bold">all</span>  <span style="color:#000;font-weight:bold">select</span>  t_id,tname
</code></pre></td></tr></table>
</div>
</div><h2 id="第四章-dml语言">第四章 DML语言</h2>
<h3 id="41插入语句">4.1、插入语句</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式一：</span>
<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>,...) <span style="color:#000;font-weight:bold">VALUES</span>(<span style="color:#a61717;background-color:#e3d2d2">值</span>,...);

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式二：</span>
<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>,<span style="color:#a61717;background-color:#e3d2d2">字段名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>,...;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、特点</strong></p>
<ol>
<li>要求值的类型和字段的类型要一致或兼容</li>
<li>字段的个数和顺序不一定与原始表中的字段个数和顺序一致，但必须保证值和字段一一对应</li>
<li>假如表中有可以为null的字段，注意可以通过以下两种方式插入null值：①字段和值都省略、②字段写上，值使用null</li>
<li>字段和值的个数必须一致</li>
<li>字段名可以省略，默认所有列</li>
<li>方式一支持一次插入多行，语法如下：INSERT INTO 表名【(字段名,…)】 VALUES(值,…),(值,…),…;</li>
<li>方式一支持子查询，语法如下：INSERT INTO 表名 查询语句;</li>
</ol>
<p><strong>三、演示</strong></p>
<p>1、方式一：插入数据</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> beauty(id,name,sex,borndate,phone,photo,boyfriend_id) 
			<span style="color:#000;font-weight:bold">VALUES</span>(<span style="color:#099">15</span>,<span style="color:#d14">&#39;唐艺昕&#39;</span>,<span style="color:#d14">&#39;女&#39;</span>,<span style="color:#d14">&#39;1997-12-05&#39;</span>,<span style="color:#d14">&#39;15633029014&#39;</span>,<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#099">2</span>);
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><p>2、方式二：插入数据</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> beauty <span style="color:#000;font-weight:bold">SET</span> 
    id <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">19</span>,name <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;张倩倩&#39;</span>,
    sex <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;女&#39;</span>,
    borndate <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;1997-12-05&#39;</span>,
    phone <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;15633029014&#39;</span>,
    photo <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">NULL</span>,
    boyfriend_id <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">3</span> ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="42修改语句">4.2、修改语句</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、单表更新</span>
<span style="color:#000;font-weight:bold">UPDATE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">列</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#a61717;background-color:#e3d2d2">值</span>,... <span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">查询条件</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、多表更新</span>
<span style="color:#000;font-weight:bold">#</span>sql92语法<span style="color:#a61717;background-color:#e3d2d2">：</span> 
<span style="color:#000;font-weight:bold">UPDATE</span> 
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span>,
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span> 
<span style="color:#000;font-weight:bold">SET</span>
  <span style="color:#a61717;background-color:#e3d2d2">列</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#a61717;background-color:#e3d2d2">值</span>,
  ...
<span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">连接条件</span> <span style="color:#000;font-weight:bold">AND</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span> ;

<span style="color:#000;font-weight:bold">#</span>sql99语法<span style="color:#a61717;background-color:#e3d2d2">：</span> 
<span style="color:#000;font-weight:bold">UPDATE</span> 
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span> 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">LEFT</span> <span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">RIGHT</span> <span style="color:#000;font-weight:bold">JOIN</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">连接条件</span>
  <span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">列</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#a61717;background-color:#e3d2d2">值</span>,
  ...
<span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span> ;
<span style="color:#099">1234567891011121314151617181920</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、演示</strong></p>
<p>1、单表更新</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">修改</span>beauty表中姓唐的女神的电话为13899888899

<span style="color:#000;font-weight:bold">UPDATE</span> beauty <span style="color:#000;font-weight:bold">SET</span> phone <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;13899888899&#39;</span> <span style="color:#000;font-weight:bold">WHERE</span> NAME <span style="color:#000;font-weight:bold">LIKE</span> <span style="color:#d14">&#39;唐%&#39;</span>;
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><p>2、多表更新</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">修改张无忌的女朋友的手机号为</span><span style="color:#099">13899888899</span><span style="color:#a61717;background-color:#e3d2d2">，魅力值为</span><span style="color:#099">1000</span>
sql92语法<span style="color:#a61717;background-color:#e3d2d2">：</span>
<span style="color:#000;font-weight:bold">UPDATE</span> 
  boys bo,
  beauty b 
  <span style="color:#000;font-weight:bold">SET</span> b.<span style="color:#000;font-weight:bold">`</span>phone<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;13899888899&#39;</span>,
      bo.<span style="color:#000;font-weight:bold">`</span>userCP<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">1000</span> 
<span style="color:#000;font-weight:bold">WHERE</span> bo.<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> b.<span style="color:#000;font-weight:bold">`</span>boyfriend_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">AND</span> bo.<span style="color:#000;font-weight:bold">`</span>boyName<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;张无忌&#39;</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">修改张无忌的女朋友的手机号为</span><span style="color:#099">13899888899</span><span style="color:#a61717;background-color:#e3d2d2">，魅力值为</span><span style="color:#099">1000</span>
sql99语法<span style="color:#a61717;background-color:#e3d2d2">：</span>
<span style="color:#000;font-weight:bold">UPDATE</span> 
  boys bo 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">JOIN</span> beauty b <span style="color:#000;font-weight:bold">ON</span> bo.<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> b.<span style="color:#000;font-weight:bold">`</span>boyfriend_id<span style="color:#000;font-weight:bold">`</span>
  <span style="color:#000;font-weight:bold">SET</span> b.<span style="color:#000;font-weight:bold">`</span>phone<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;13899888899&#39;</span>,
      bo.<span style="color:#000;font-weight:bold">`</span>userCP<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">1000</span> 
<span style="color:#000;font-weight:bold">WHERE</span> bo.<span style="color:#000;font-weight:bold">`</span>boyName<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;张无忌&#39;</span> ;
<span style="color:#099">1234567891011121314151617</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="43删除语句">4.3、删除语句</h3>
<p><strong>一、语法</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、单表删除</span> 
<span style="color:#000;font-weight:bold">DELETE</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span> <span style="color:#a61717;background-color:#e3d2d2">】</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、多表删除（级联删除）</span>
sql92语法<span style="color:#a61717;background-color:#e3d2d2">：</span> 
<span style="color:#000;font-weight:bold">DELETE</span> 
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">的别名</span>,
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">的别名</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span>,
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span> 
<span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">连接条件</span> <span style="color:#000;font-weight:bold">AND</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span> ;

sql99语法<span style="color:#a61717;background-color:#e3d2d2">：</span> 
<span style="color:#000;font-weight:bold">DELETE</span> 
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">的别名</span>,
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">的别名</span> 
<span style="color:#000;font-weight:bold">FROM</span>
  <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">1</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span> 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">LEFT</span> <span style="color:#000;font-weight:bold">|</span> <span style="color:#000;font-weight:bold">RIGHT</span> <span style="color:#000;font-weight:bold">JOIN</span> <span style="color:#a61717;background-color:#e3d2d2">表</span><span style="color:#099">2</span> <span style="color:#a61717;background-color:#e3d2d2">别名</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">连接条件</span> 
<span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件</span> ;
<span style="color:#099">123456789101112131415161718192021</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、演示</strong></p>
<p>1、单表删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除手机号以</span><span style="color:#099">9</span><span style="color:#a61717;background-color:#e3d2d2">结尾的女神信息</span>

<span style="color:#000;font-weight:bold">DELETE</span> <span style="color:#000;font-weight:bold">FROM</span> beauty <span style="color:#000;font-weight:bold">WHERE</span> phone <span style="color:#000;font-weight:bold">LIKE</span> <span style="color:#d14">&#39;%9&#39;</span>;
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><p>2、多表删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除黄晓明的信息以及他女朋友的信息</span>
<span style="color:#000;font-weight:bold">#</span>sql92语法<span style="color:#a61717;background-color:#e3d2d2">：</span>
<span style="color:#000;font-weight:bold">DELETE</span>
  b,
  bo
<span style="color:#000;font-weight:bold">FROM</span>
  beauty b,
  boys bo
<span style="color:#000;font-weight:bold">WHERE</span> b.<span style="color:#000;font-weight:bold">`</span>boyfriend_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> bo.<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">AND</span> bo.<span style="color:#000;font-weight:bold">`</span>boyName<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;黄晓明&#39;</span> ; 

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除黄晓明的信息以及他女朋友的信息</span>
<span style="color:#000;font-weight:bold">#</span>sql99语法<span style="color:#a61717;background-color:#e3d2d2">：</span>
<span style="color:#000;font-weight:bold">DELETE</span> 
  b,
  bo 
<span style="color:#000;font-weight:bold">FROM</span>
  beauty b 
<span style="color:#000;font-weight:bold">INNER</span> <span style="color:#000;font-weight:bold">JOIN</span> boys bo <span style="color:#000;font-weight:bold">ON</span> b.<span style="color:#000;font-weight:bold">`</span>boyfriend_id<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> bo.<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span> 
<span style="color:#000;font-weight:bold">WHERE</span> bo.<span style="color:#000;font-weight:bold">`</span>boyName<span style="color:#000;font-weight:bold">`</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#d14">&#39;黄晓明&#39;</span> ;
<span style="color:#099">12345678910111213141516171819</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="第五章-ddl语言">第五章 DDL语言</h2>
<h3 id="51库的管理">5.1、库的管理</h3>
<h4 id="511库的创建">5.1.1、库的创建</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">DATABASE</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">EXISTS</span><span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#a61717;background-color:#e3d2d2">库名</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#0086b3">CHARACTER</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">字符集名】</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="512库的修改">5.1.2、库的修改</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">它已经被废弃</span>
<span style="color:#000;font-weight:bold">RENAME</span> <span style="color:#000;font-weight:bold">DATABASE</span> <span style="color:#a61717;background-color:#e3d2d2">旧库名</span> <span style="color:#000;font-weight:bold">TO</span> <span style="color:#a61717;background-color:#e3d2d2">新库名</span>;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">修改库字符集</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">DATABASE</span> <span style="color:#a61717;background-color:#e3d2d2">库名</span> <span style="color:#0086b3">CHARACTER</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">字符集名</span>;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="513库的删除">5.1.3、库的删除</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">DATABASE</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">EXISTS</span><span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#a61717;background-color:#e3d2d2">库名</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="52表的管理">5.2、表的管理</h3>
<h4 id="521表的创建">5.2.1、表的创建</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">EXISTS</span><span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> (
  <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#a61717;background-color:#e3d2d2">【约束】</span>,
  <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#a61717;background-color:#e3d2d2">【约束】</span>,
  ...
  <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#a61717;background-color:#e3d2d2">【约束】</span>
) ;
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="522表的修改">5.2.2、表的修改</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、添加列</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">ADD</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">列名</span> <span style="color:#a61717;background-color:#e3d2d2">类型</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">FIRST</span><span style="color:#000;font-weight:bold">|</span><span style="color:#000;font-weight:bold">AFTER</span> <span style="color:#a61717;background-color:#e3d2d2">字段名】</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、修改列的类型或约束</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">列名</span> <span style="color:#a61717;background-color:#e3d2d2">新类型</span> <span style="color:#a61717;background-color:#e3d2d2">【新约束】</span>;

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、修改列名</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> CHANGE <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">旧列名</span> <span style="color:#a61717;background-color:#e3d2d2">新列名</span> <span style="color:#a61717;background-color:#e3d2d2">类型</span>;

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、删除列</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">列名</span>;

<span style="color:#099">5</span><span style="color:#a61717;background-color:#e3d2d2">、修改表名</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">RENAME</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">TO</span><span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#a61717;background-color:#e3d2d2">新表名</span>;
<span style="color:#099">1234567891011121314</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="523表的删除">5.2.3、表的删除</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">方式一：</span><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">EXISTS</span><span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;

<span style="color:#a61717;background-color:#e3d2d2">方式二：</span><span style="color:#000;font-weight:bold">TRUNCATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">EXISTS</span><span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="524表的复制">5.2.4、表的复制</h4>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、复制表的结构</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">LIKE</span> <span style="color:#a61717;background-color:#e3d2d2">旧表</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、复制表的某些字段</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> 
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">字段</span><span style="color:#099">1</span>,<span style="color:#a61717;background-color:#e3d2d2">字段</span><span style="color:#099">2</span>,... <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">旧表</span> <span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#099">0</span>;

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、复制表的结构</span><span style="color:#000;font-weight:bold">+</span><span style="color:#a61717;background-color:#e3d2d2">数据</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> 
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">查询列表</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">旧表</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件】</span>;

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、复制表的某些字段</span><span style="color:#000;font-weight:bold">+</span><span style="color:#a61717;background-color:#e3d2d2">数据</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> 
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">字段</span><span style="color:#099">1</span>,<span style="color:#a61717;background-color:#e3d2d2">字段</span><span style="color:#099">2</span>,... <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">旧表</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">WHERE</span> <span style="color:#a61717;background-color:#e3d2d2">筛选条件】</span>;
<span style="color:#099">1234567891011121314</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="53数据类型">5.3、数据类型</h3>
<h4 id="531数值型">5.3.1、数值型</h4>
<p><strong>一、类型</strong></p>
<table>
<thead>
<tr>
<th>类型</th>
<th>TINYINT</th>
<th>SMALLINT</th>
<th>MEDIUMINT</th>
<th>INT/INTEGER</th>
<th>BIGINT</th>
</tr>
</thead>
<tbody>
<tr>
<td>字节</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>8</td>
</tr>
</tbody>
</table>
<p><strong>二、特点</strong></p>
<ol>
<li>都可以设置无符号和有符号，默认有符号，通过unsigned设置无符号</li>
<li>如果超出了范围，会报out or range异常，插入临界值（该类型的最大值或最小值即为临界值）</li>
<li>长度可以不指定，默认会有一个长度，长度代表显示的最大宽度，如果不够则左边用0填充，但需要搭配zerofill，并且默认变为无符号整型</li>
<li>如果对数据没有特殊要求，则优先考虑使用INT/INTEGER</li>
</ol>
<h4 id="532浮点型">5.3.2、浮点型</h4>
<p><strong>一、类型</strong></p>
<ol>
<li>定点数
<ul>
<li>DEC(M,D) ：M+2字节</li>
<li>DECIMAL(M,D)：M+2字节</li>
</ul>
</li>
<li>浮点数
<ul>
<li>FLOAT(M,D) ：4字节</li>
<li>DOUBLE(M,D)：8字节</li>
</ul>
</li>
</ol>
<p><strong>二、特点</strong></p>
<ol>
<li>M代表整数部位+小数部位的个数，D代表小数部位</li>
<li>如果超出范围，则报out or range异常，并且插入临界值（该类型的最大值或最小值即为临界值）</li>
<li>M和D都可以省略，但对于定点数，M默认为10，D默认为0</li>
<li>如果精度要求较高，则优先考虑使用定点数</li>
</ol>
<h4 id="533字符型">5.3.3、字符型</h4>
<p><strong>一、类型</strong></p>
<table>
<thead>
<tr>
<th>类型</th>
<th>CHAR</th>
<th>VARCHAR</th>
<th>BINARY</th>
<th>VARBINARY</th>
<th>ENUM</th>
<th>SET</th>
<th>TEXT</th>
<th>BLOB</th>
</tr>
</thead>
<tbody>
<tr>
<td>描述</td>
<td>固定长度字符</td>
<td>可变长度字符</td>
<td>二进制字符串</td>
<td>二进制字符串</td>
<td>枚举</td>
<td>集合</td>
<td>文本</td>
<td>二进制大型对象</td>
</tr>
</tbody>
</table>
<p><strong>二、特点</strong></p>
<ol>
<li>char：固定长度的字符，写法为char(M)，最大长度不能超过M，其中M可以省略，默认为1</li>
<li>varchar：可变长度的字符，写法为varchar(M)，最大长度不能超过M，其中M不可以省略</li>
<li>如果对数据没有特殊要求，则优先考虑使用VARCHAR</li>
</ol>
<h4 id="534日期型">5.3.4、日期型</h4>
<p><strong>一、类型</strong></p>
<table>
<thead>
<tr>
<th>类型</th>
<th>YEAR</th>
<th>DATE</th>
<th>TIME</th>
<th>DATETIME</th>
<th>TIMESTAMP</th>
</tr>
</thead>
<tbody>
<tr>
<td>描述</td>
<td>年份</td>
<td>日期</td>
<td>时间</td>
<td>日期+时间</td>
<td>日期+时间</td>
</tr>
</tbody>
</table>
<p><strong>二、特点</strong></p>
<ol>
<li>TIMESTAMP比较容易受时区、语法模式、版本的影响，更能反映当前时区的真实时间，而DATETIME则只能反映出插入时的当地时区</li>
<li>TIMESTAMP支持的时间范围较小，DATETIME的取值范围：1000-1-1 — 9999-12-31</li>
<li>TIMESTAMP的属性受Mysql版本和SQLMode的影响很大</li>
<li>如果对数据没有特殊要求，则优先考虑使用DATETIME</li>
</ol>
<h3 id="54常见约束">5.4、常见约束</h3>
<p><strong>一、含义</strong></p>
<p>约束是一种限制，用于限制表中的数据，为了保证表中的数据的准确和可靠性</p>
<p><strong>二、分类</strong></p>
<ol>
<li>NOT NULL：非空，该字段的值必填</li>
<li>UNIQUE：唯一，该字段的值不可重复</li>
<li>DEFAULT：默认，该字段的值不用手动插入有默认值</li>
<li>CHECK：检查，MySQL不支持</li>
<li>PRIMARY KEY：主键，该字段的值不可重复并且非空 unique+not null</li>
<li>FOREIGN KEY：外键，该字段的值引用了另外的表的字段</li>
</ol>
<p><strong>三、特点</strong></p>
<p>主键和唯一</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">不同点：</span>
<span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、一个表至多有一个主键，但可以有多个唯一</span>
<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、主键不允许为空，唯一可以为空</span>

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">相同点：</span>
<span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、都具有唯一性</span>
<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、都支持组合键，但不推荐</span>
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><p>外键</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、用于限制两个表的关系，从表的字段值引用了主表的某字段值</span>
<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、外键列和主表的被引用列要求类型一致，意义一样，名称无要求</span>
<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、主表的被引用列要求是一个</span><span style="color:#000;font-weight:bold">key</span><span style="color:#a61717;background-color:#e3d2d2">（一般就是主键）</span>
<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、插入数据，先插入主表；删除数据，先删除从表</span>


<span style="color:#a61717;background-color:#e3d2d2">可以通过以下两种方式来删除主表的记录</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式一：级联删除</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> stuinfo <span style="color:#000;font-weight:bold">ADD</span> <span style="color:#000;font-weight:bold">CONSTRAINT</span> fk_stu_major <span style="color:#000;font-weight:bold">FOREIGN</span> <span style="color:#000;font-weight:bold">KEY</span>(majorid) <span style="color:#000;font-weight:bold">REFERENCES</span> major(id) <span style="color:#000;font-weight:bold">ON</span> <span style="color:#000;font-weight:bold">DELETE</span> <span style="color:#000;font-weight:bold">CASCADE</span>;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式二：级联置空</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> stuinfo <span style="color:#000;font-weight:bold">ADD</span> <span style="color:#000;font-weight:bold">CONSTRAINT</span> fk_stu_major <span style="color:#000;font-weight:bold">FOREIGN</span> <span style="color:#000;font-weight:bold">KEY</span>(majorid) <span style="color:#000;font-weight:bold">REFERENCES</span> major(id) <span style="color:#000;font-weight:bold">ON</span> <span style="color:#000;font-weight:bold">DELETE</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">NULL</span>;
<span style="color:#099">123456789101112</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>四、演示</strong></p>
<p>1、创建表时添加约束</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>(
	<span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">非空</span>
    <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#a61717;background-color:#e3d2d2">值</span>,<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">默认</span>
	<span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span>,<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">主键</span>
	<span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">UNIQUE</span>,<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">唯一</span>
	<span style="color:#000;font-weight:bold">CONSTRAINT</span> <span style="color:#a61717;background-color:#e3d2d2">约束名</span> <span style="color:#000;font-weight:bold">FOREIGN</span> <span style="color:#000;font-weight:bold">KEY</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>) <span style="color:#000;font-weight:bold">REFERENCES</span> <span style="color:#a61717;background-color:#e3d2d2">主表</span>(<span style="color:#a61717;background-color:#e3d2d2">被引用列</span>)
) ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><p>注意：</p>
<ol>
<li>列级约束支持：非空、默认、主键、唯一，不可以起约束名</li>
<li>表级约束支持：主键、唯一、外键，可以起约束名，但是在MySQL中对主键无效</li>
<li>列级约束可以在一个字段上追加多个，中间用空格隔开，没有顺序要求</li>
</ol>
<p>2、修改表时添加或删除约束</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">30
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">31
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">32
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">33
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">34
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、非空</span>
<span style="color:#a61717;background-color:#e3d2d2">添加非空（列级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span>;
<span style="color:#a61717;background-color:#e3d2d2">删除非空</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、默认</span>
<span style="color:#a61717;background-color:#e3d2d2">添加默认（列级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#a61717;background-color:#e3d2d2">值</span>;
<span style="color:#a61717;background-color:#e3d2d2">删除默认</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span>;

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、主键</span>
<span style="color:#a61717;background-color:#e3d2d2">添加主键（列级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span>;
<span style="color:#a61717;background-color:#e3d2d2">添加主键（表级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">add</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">CONSTRAINT</span> <span style="color:#a61717;background-color:#e3d2d2">约束名】</span> <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>);
<span style="color:#a61717;background-color:#e3d2d2">删除主键</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span>;

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、唯一</span>
<span style="color:#a61717;background-color:#e3d2d2">添加唯一（列级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#000;font-weight:bold">UNIQUE</span>;
<span style="color:#a61717;background-color:#e3d2d2">添加唯一（表级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">add</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">CONSTRAINT</span> <span style="color:#a61717;background-color:#e3d2d2">约束名】</span> <span style="color:#000;font-weight:bold">UNIQUE</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>);
<span style="color:#a61717;background-color:#e3d2d2">删除唯一</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">INDEX</span> <span style="color:#a61717;background-color:#e3d2d2">索引名</span>;

<span style="color:#099">5</span><span style="color:#a61717;background-color:#e3d2d2">、外键</span>
<span style="color:#a61717;background-color:#e3d2d2">添加外键（表级约束）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">add</span> <span style="color:#a61717;background-color:#e3d2d2">【</span><span style="color:#000;font-weight:bold">CONSTRAINT</span> <span style="color:#a61717;background-color:#e3d2d2">约束名】</span> <span style="color:#000;font-weight:bold">FOREIGN</span> <span style="color:#000;font-weight:bold">KEY</span>(<span style="color:#a61717;background-color:#e3d2d2">字段名</span>) <span style="color:#000;font-weight:bold">REFERENCES</span> <span style="color:#a61717;background-color:#e3d2d2">主表</span>(<span style="color:#a61717;background-color:#e3d2d2">被引用列</span>);
<span style="color:#a61717;background-color:#e3d2d2">删除外键</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">FOREIGN</span> <span style="color:#000;font-weight:bold">KEY</span> <span style="color:#a61717;background-color:#e3d2d2">约束名</span>;
<span style="color:#099">123456789101112131415161718192021222324252627282930313233</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="55自增长列">5.5、自增长列</h3>
<p><strong>一、含义</strong></p>
<p>自增长列又称为标识列，它可以不用手动的插入值，系统提供默认的序列值</p>
<p><strong>二、特点</strong></p>
<ol>
<li>不用手动插入值，可以自动提供序列值，默认从1开始，步长为1，如果要更改起始值，第一次手动插入值，后续使用NULL，如果要更改步长，修改更改系统变量：SET auto_increment_increment = 值;</li>
<li>一个表至多有一个自增长列</li>
<li>自增长列只能支持数值型</li>
<li>自增长列必须为一个key</li>
</ol>
<p><strong>三、演示</strong></p>
<p>1、创建表时添加自增长列</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> (
  <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#a61717;background-color:#e3d2d2">约束</span> AUTO_INCREMENT
) ;
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><p>2、修改表时添加或删除自增长列</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">添加自增长列</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#a61717;background-color:#e3d2d2">约束</span> AUTO_INCREMENT;

<span style="color:#a61717;background-color:#e3d2d2">删除自增长列</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表</span> <span style="color:#000;font-weight:bold">MODIFY</span> <span style="color:#000;font-weight:bold">COLUMN</span> <span style="color:#a61717;background-color:#e3d2d2">字段名</span> <span style="color:#a61717;background-color:#e3d2d2">字段类型</span> <span style="color:#a61717;background-color:#e3d2d2">约束</span>;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="第六章-dcl语言">第六章 DCL语言</h2>
<p>关于授权的权限列表：</p>
<p>
        <a data-fancybox="gallery" href="https://i.loli.net/2020/12/24/7fchtpPqbZjXBUD.png">
            <img class="mx-auto" alt="image-20200809204539412" src="https://i.loli.net/2020/12/24/7fchtpPqbZjXBUD.png" />
        </a>
    </p>
<h3 id="61创建用户">6.1、创建用户</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">USER</span> <span style="color:#a61717;background-color:#e3d2d2">用户名</span><span style="color:#000;font-weight:bold">@</span><span style="color:#d14">&#39;IP地址&#39;</span> IDENTIFIED <span style="color:#000;font-weight:bold">BY</span> <span style="color:#d14">&#39;密码&#39;</span>;
<span style="color:#a61717;background-color:#e3d2d2">注意：</span><span style="color:#d14">&#39;IP地址&#39;</span><span style="color:#a61717;background-color:#e3d2d2">可以设置为</span>localhost(<span style="color:#a61717;background-color:#e3d2d2">代表本机</span>)<span style="color:#a61717;background-color:#e3d2d2">或者</span><span style="color:#d14">&#39;%&#39;</span>(<span style="color:#a61717;background-color:#e3d2d2">代表允许所有</span>IP地址登录)
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="62删除用户">6.2、删除用户</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">USER</span> <span style="color:#a61717;background-color:#e3d2d2">用户名</span><span style="color:#000;font-weight:bold">@</span><span style="color:#d14">&#39;IP地址&#39;</span>;
<span style="color:#a61717;background-color:#e3d2d2">注意：</span><span style="color:#d14">&#39;IP地址&#39;</span><span style="color:#a61717;background-color:#e3d2d2">可以设置为</span>localhost(<span style="color:#a61717;background-color:#e3d2d2">代表本机</span>)<span style="color:#a61717;background-color:#e3d2d2">或者</span><span style="color:#d14">&#39;%&#39;</span>(<span style="color:#a61717;background-color:#e3d2d2">代表允许所有</span>IP地址登录)
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="63用户授权">6.3、用户授权</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">GRANT</span> <span style="color:#a61717;background-color:#e3d2d2">权限</span><span style="color:#099">1</span>,<span style="color:#a61717;background-color:#e3d2d2">权限</span><span style="color:#099">2</span>,...... <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">数据库名</span>.<span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">TO</span> <span style="color:#a61717;background-color:#e3d2d2">用户名</span><span style="color:#000;font-weight:bold">@</span><span style="color:#d14">&#39;IP地址&#39;</span> IDENTIFIED <span style="color:#000;font-weight:bold">BY</span> <span style="color:#d14">&#39;密码&#39;</span>;
<span style="color:#a61717;background-color:#e3d2d2">注意：所有的数据库就用</span><span style="color:#000;font-weight:bold">*</span>.<span style="color:#000;font-weight:bold">*</span><span style="color:#a61717;background-color:#e3d2d2">，所有的权限就用</span>all或者all <span style="color:#000;font-weight:bold">privileges</span>
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="64撤销授权">6.4、撤销授权</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">REVOKE</span> <span style="color:#a61717;background-color:#e3d2d2">权限</span><span style="color:#099">1</span>,<span style="color:#a61717;background-color:#e3d2d2">权限</span><span style="color:#099">2</span>,...... <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">数据库名</span>.<span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">用户名</span><span style="color:#000;font-weight:bold">@</span><span style="color:#d14">&#39;IP地址&#39;</span> IDENTIFIED <span style="color:#000;font-weight:bold">BY</span> <span style="color:#d14">&#39;密码&#39;</span>;
<span style="color:#a61717;background-color:#e3d2d2">注意：所有的数据库就用</span><span style="color:#000;font-weight:bold">*</span>.<span style="color:#000;font-weight:bold">*</span><span style="color:#a61717;background-color:#e3d2d2">，所有的权限就用</span>all或者all <span style="color:#000;font-weight:bold">privileges</span>
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="65刷新授权">6.5、刷新授权</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql">FLUSH <span style="color:#000;font-weight:bold">PRIVILEGES</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="66查看授权">6.6、查看授权</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SHOW</span> GRANTS <span style="color:#000;font-weight:bold">FOR</span> <span style="color:#a61717;background-color:#e3d2d2">用户名</span><span style="color:#000;font-weight:bold">@</span><span style="color:#d14">&#39;IP地址&#39;</span>;
<span style="color:#a61717;background-color:#e3d2d2">注意：</span><span style="color:#d14">&#39;IP地址&#39;</span><span style="color:#a61717;background-color:#e3d2d2">可以设置为</span>localhost(<span style="color:#a61717;background-color:#e3d2d2">代表本机</span>)<span style="color:#a61717;background-color:#e3d2d2">或者</span><span style="color:#d14">&#39;%&#39;</span>(<span style="color:#a61717;background-color:#e3d2d2">代表允许所有</span>IP地址登录)
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="67修改密码">6.7、修改密码</h3>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">修改密码</span>
<span style="color:#000;font-weight:bold">SET</span> PASSWORD <span style="color:#000;font-weight:bold">=</span> PASSWORD(<span style="color:#d14">&#39;123456&#39;</span>);
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">登录授权</span>
<span style="color:#000;font-weight:bold">GRANT</span> <span style="color:#000;font-weight:bold">ALL</span> <span style="color:#000;font-weight:bold">PRIVILEGES</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#000;font-weight:bold">*</span>.<span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">TO</span> <span style="color:#d14">&#39;root&#39;</span><span style="color:#000;font-weight:bold">@</span><span style="color:#d14">&#39;%&#39;</span> IDENTIFIED <span style="color:#000;font-weight:bold">BY</span> <span style="color:#d14">&#39;123456&#39;</span>;
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">刷新授权</span>
FLUSH <span style="color:#000;font-weight:bold">PRIVILEGES</span>;
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="68忘记密码">6.8、忘记密码</h3>
<pre><code>1、可以在配置文件里加上 skip-grant-tables ，注意写到[mysqld]参数组下，表示跳过授权
2、重启MySQL再登录就不需要密码，进去改密码，改完后，直接 FLUSH PRIVILEGES; 就可以使用新密码来登录了
（例：UPDATE mysql.user SET PASSWORD=PASSWORD(&quot;123456&quot;) WHERE USER=&quot;root&quot; AND HOST=&quot;localhost&quot;;）
3、改完后记得去掉配置文件例的 skip-grant-tables，重新启动MySQL服务
4、再使用新的密码登录就可以了
12345
</code></pre><h2 id="第七章-tcl语言">第七章 TCL语言</h2>
<h3 id="71事务">7.1、事务</h3>
<p><strong>一、含义</strong></p>
<p>一条或多条sql语句组成一个执行单位，一组sql语句要么都执行要么都不执行</p>
<p><strong>二、特点（ACID）</strong></p>
<ol>
<li>原子性：一个事务是不可再分割的整体，要么都执行要么都不执行</li>
<li>一致性：一个事务的执行不能破坏数据库数据的完整性和一致性</li>
<li>隔离性：一个事务不受其它事务的干扰，多个事务是互相隔离的</li>
<li>持久性：一个事务一旦提交了，则永久的持久化到本地</li>
</ol>
<p><strong>三、分类</strong></p>
<ol>
<li>隐式事务：没有明显的开启和结束，本身就是一条事务可以自动提交，比如insert、update、delete</li>
<li>显式事务：具有明显的开启和结束，例如以下格式：</li>
</ol>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、开启事务</span>
<span style="color:#000;font-weight:bold">set</span> autocommit<span style="color:#000;font-weight:bold">=</span><span style="color:#099">0</span>;<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">关闭自动提交</span>
<span style="color:#000;font-weight:bold">start</span> <span style="color:#000;font-weight:bold">transaction</span>;<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">开启事务机制</span>

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、编写一组逻辑</span>sql语句
<span style="color:#a61717;background-color:#e3d2d2">注意：</span>sql语句支持的是insert<span style="color:#a61717;background-color:#e3d2d2">、</span><span style="color:#000;font-weight:bold">update</span><span style="color:#a61717;background-color:#e3d2d2">、</span><span style="color:#000;font-weight:bold">delete</span>

<span style="color:#a61717;background-color:#e3d2d2">【设置回滚点，可选项】</span>
savepoint <span style="color:#a61717;background-color:#e3d2d2">回滚点名</span>;

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、结束事务</span>
<span style="color:#a61717;background-color:#e3d2d2">提交：</span><span style="color:#000;font-weight:bold">commit</span>;
<span style="color:#a61717;background-color:#e3d2d2">回滚：</span><span style="color:#000;font-weight:bold">rollback</span>;
<span style="color:#a61717;background-color:#e3d2d2">回滚到指定的地方：</span> <span style="color:#000;font-weight:bold">rollback</span> <span style="color:#000;font-weight:bold">to</span> <span style="color:#a61717;background-color:#e3d2d2">回滚点名</span>;
<span style="color:#099">1234567891011121314</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="72事务并发读问题">7.2、事务并发（读问题）</h3>
<p><strong>一、事物的并发问题如何发生？</strong></p>
<p>多个事务同时操作同一个数据库的相同数据时</p>
<p><strong>二、事务的并发问题都有哪些？</strong></p>
<ol>
<li>脏读：一个事务读到了另一个事务还未提交的update数据，导致多次查询的结果不一样</li>
<li>不可重复读：一个事务读到了另一个事务已经提交的update数据，导致多次查询结果不一致</li>
<li>幻读：一个事务读到了另一个事务已经提交的insert数据，导致多次查询的结果不一样</li>
</ol>
<p><strong>三、事物的并发问题如何解决？</strong></p>
<p>通过设置隔离级别来解决并发问题</p>
<p><strong>四、隔离级别</strong></p>
<table>
<thead>
<tr>
<th>隔离级别</th>
<th>隔离描述</th>
<th>脏读</th>
<th>不可重复读</th>
<th>幻读</th>
</tr>
</thead>
<tbody>
<tr>
<td>READ UNCOMMITTED</td>
<td>读未提交</td>
<td>×</td>
<td>×</td>
<td>×</td>
</tr>
<tr>
<td>READ COMMITTED</td>
<td>读已提交</td>
<td>√</td>
<td>×</td>
<td>×</td>
</tr>
<tr>
<td>REPEATABLE READ</td>
<td>可重复读</td>
<td>√</td>
<td>√</td>
<td>×</td>
</tr>
<tr>
<td>SERIALIZABLE</td>
<td>串行化</td>
<td>√</td>
<td>√</td>
<td>√</td>
</tr>
</tbody>
</table>
<p><strong>五、注意问题</strong></p>
<ol>
<li>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql">mysql <span style="color:#a61717;background-color:#e3d2d2">中默认第三个隔离级别</span> <span style="color:#000;font-weight:bold">REPEATABLE</span> <span style="color:#000;font-weight:bold">READ</span>
oracle中默认第二个隔离级别 <span style="color:#000;font-weight:bold">READ</span> <span style="color:#000;font-weight:bold">COMMITTED</span>
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div></li>
<li>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">查看隔离级别</span> <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@@</span>tx_isolation;
<span style="color:#a61717;background-color:#e3d2d2">设置隔离级别</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">SESSION</span><span style="color:#000;font-weight:bold">|</span><span style="color:#000;font-weight:bold">GLOBAL</span> <span style="color:#000;font-weight:bold">TRANSACTION</span> <span style="color:#000;font-weight:bold">ISOLATION</span> <span style="color:#000;font-weight:bold">LEVEL</span> <span style="color:#a61717;background-color:#e3d2d2">隔离级别</span>;
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div></li>
</ol>
<h3 id="73丢失更新写问题">7.3、丢失更新（写问题）</h3>
<p><strong>一、定义</strong></p>
<p>在事务的隔离级别内容中，能够了解到两个不同的事务在并发的时候可能会发生数据的影响。细心的话可以发现事务隔离级别章节中，脏读、不可重复读、幻读三个问题都是由事务A对数据进行修改、增加，事务B总是在做读操作。如果两事务都在对数据进行修改则会导致另外的问题：丢失更新。</p>
<p><strong>二、解决</strong></p>
<ol>
<li>悲观锁：认为两个事务更新操作一定会发生丢失更新
<ul>
<li>解决：通过在语句后边添加for update来实现行级上锁，所以又称为“行级锁”，例如：select * from t_account t wheret.id=‘1’ for update;</li>
</ul>
</li>
<li>乐观锁：认为事务不一定会产生丢失更新，让事务进行并发修改，不对事务进行锁定
<ul>
<li>解决：由程序员自己解决，可以通过给数据表添加自增的version字段或时间戳timestamp，进行数据修改时，数据库会检测version字段或者时间戳是否与原来的一致，若不一致，抛出异常或者重新查询</li>
</ul>
</li>
</ol>
<p><strong>三、注意</strong></p>
<p>对于账户交易建议直接使用悲观锁，数据库的性能很高，并发度不是很高的场景两者性能没有太大差别。如果是交易减库存的操作可以考虑乐观锁，保证并发度。</p>
<h2 id="第八章-高级部分">第八章 高级部分</h2>
<h3 id="81索引">8.1、索引</h3>
<p><strong>一、含义</strong></p>
<p>索引（index）是帮助MySQL高效获取数据的一种有序的数据结构</p>
<p><strong>二、特点</strong></p>
<ol>
<li>优势：
<ul>
<li>类似于书籍的目录索引，提高数据检索的效率，降低数据库的IO成本</li>
<li>通过索引列对数据进行排序，降低数据排序的成本，降低CPU的消耗</li>
</ul>
</li>
<li>劣势：
<ul>
<li>实际上索引也是一张表，该表中保存了主键与索引字段，并指向实体类的记录，所以索引列也是要占用空间的</li>
<li>虽然索引大大提高了查询效率，同时却也降低更新表的速度，如对表进行INSERT、UPDATE、DELETE。因为更新表时，MySQL 不仅要保存数据，还要保存一下索引文件每次更新添加了索引列的字段，都会调整因为更新所带来的键值变化后的索引信息</li>
</ul>
</li>
</ol>
<p><strong>三、语法</strong></p>
<p>1、创建</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">UNIQUE</span><span style="color:#000;font-weight:bold">|</span>FULLTEXT<span style="color:#000;font-weight:bold">|</span>SPATIAL <span style="color:#a61717;background-color:#e3d2d2">】</span> <span style="color:#000;font-weight:bold">INDEX</span> <span style="color:#a61717;background-color:#e3d2d2">索引名称</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>(<span style="color:#a61717;background-color:#e3d2d2">字段列表</span>);
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>2、修改</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">先删除，在创建</span>
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>3、删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">INDEX</span> <span style="color:#a61717;background-color:#e3d2d2">索引名称</span> <span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#000;font-weight:bold">INDEX</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>5、alter命令</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">该语句添加一个主键，这意味着索引值必须是唯一的，且不能为</span><span style="color:#000;font-weight:bold">NULL</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">ADD</span> <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span>(<span style="color:#a61717;background-color:#e3d2d2">字段列表</span>); 
	
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">这条语句创建索引的值必须是唯一的（除了</span>NULL外<span style="color:#a61717;background-color:#e3d2d2">，</span>NULL可能会出现多次<span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">ADD</span> <span style="color:#000;font-weight:bold">UNIQUE</span> <span style="color:#a61717;background-color:#e3d2d2">索引名称</span>(<span style="color:#a61717;background-color:#e3d2d2">字段列表</span>);
	
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">添加普通索引，索引值可以出现多次。</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">ADD</span> <span style="color:#000;font-weight:bold">INDEX</span> <span style="color:#a61717;background-color:#e3d2d2">索引名称</span>(<span style="color:#a61717;background-color:#e3d2d2">字段列表</span>);
	
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">该语句指定了索引为</span>FULLTEXT<span style="color:#a61717;background-color:#e3d2d2">，用于全文索引</span>	
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">TABLE</span> <span style="color:#a61717;background-color:#e3d2d2">表名</span> <span style="color:#000;font-weight:bold">ADD</span> FULLTEXT <span style="color:#a61717;background-color:#e3d2d2">索引名称</span>(<span style="color:#a61717;background-color:#e3d2d2">字段列表</span>);
<span style="color:#099">1234567891011</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>四、注意</strong></p>
<p>索引的设计可以遵循一些已有的原则，创建索引的时候请尽量考虑符合这些原则，便于提升索引的使用效率，更高效的使用索引：</p>
<ol>
<li>索引字段的选择，最佳候选列应当从where子句的条件中提取，如果where子句中的组合比较多，那么应当挑选最常用、过滤效果最好的列的组合</li>
<li>索引可以有效的提升查询数据的效率，但索引数量不是多多益善，索引越多，维护索引的代价自然也就水涨船高。对于插入、更新、删除等DML操作比较频繁的表来说，索引过多，会引入相当高的维护代价，降低DML操作的效率，增加相应操作的时间消耗。另外索引过多的话，MySQL也会犯选择困难病，虽然最终仍然会找到一个可用的索引，但无疑提高了选择的代价</li>
<li>对查询频次较高，且数据量比较大的表建立索引</li>
<li>使用唯一索引，区分度越高，使用索引的效率越高</li>
<li>使用短索引，索引创建之后也是使用硬盘来存储的，因此提升索引访问的I/O效率，也可以提升总体的访问效率。假如构成索引的字段总长度比较短，那么在给定大小的存储块内可以存储更多的索引值，相应的可以有效的提升MySQL访问索引的I/O效率</li>
<li>使用组合索引，如果查询时where子句中使用了组成该索引的前几个字段，那么这条查询SQL可以利用组合索引来提升查询效率。例如：CREATE INDEX idx_name_email_status ON tb_seller(name,email,status); 就相当于对name 创建索引；对name , email 创建了索引；对name , email, status 创建了索引</li>
</ol>
<h3 id="82视图">8.2、视图</h3>
<p><strong>一、含义</strong></p>
<p>MySQL在5.1以后推出了视图（VIEW），本身是一个虚拟表，它的数据来自于表，通过执行时动态生成</p>
<p><strong>二、特点</strong></p>
<ol>
<li>简化sql语句</li>
<li>提高了sql的重用性</li>
<li>保护基表的数据，提高了安全性</li>
</ol>
<p><strong>三、语法</strong></p>
<p>1、创建</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">VIEW</span> <span style="color:#a61717;background-color:#e3d2d2">视图名</span>
<span style="color:#000;font-weight:bold">AS</span>
<span style="color:#a61717;background-color:#e3d2d2">查询语句</span>;
<span style="color:#099">123</span>
</code></pre></td></tr></table>
</div>
</div><p>2、修改</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式一：</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">OR</span> <span style="color:#000;font-weight:bold">REPLACE</span> <span style="color:#000;font-weight:bold">VIEW</span> <span style="color:#a61717;background-color:#e3d2d2">视图名</span>
<span style="color:#000;font-weight:bold">AS</span>
<span style="color:#a61717;background-color:#e3d2d2">查询语句</span>;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式二：</span>
<span style="color:#000;font-weight:bold">ALTER</span> <span style="color:#000;font-weight:bold">VIEW</span> <span style="color:#a61717;background-color:#e3d2d2">视图名</span>
<span style="color:#000;font-weight:bold">AS</span>
<span style="color:#a61717;background-color:#e3d2d2">查询语句</span>;
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p>3、删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">VIEW</span> <span style="color:#a61717;background-color:#e3d2d2">视图</span><span style="color:#099">1</span>,<span style="color:#a61717;background-color:#e3d2d2">视图</span><span style="color:#099">2</span>,...;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式一：</span>
<span style="color:#000;font-weight:bold">DESC</span> <span style="color:#a61717;background-color:#e3d2d2">视图名</span>;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">方式二：</span>
<span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">VIEW</span> <span style="color:#a61717;background-color:#e3d2d2">视图名</span>;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>四、注意</strong></p>
<p>视图一般用于查询的，而不是更新的，所以具备以下特点的视图都不允许更新：</p>
<ol>
<li>包含分组函数、group by、distinct、having、union、join</li>
<li>常量视图</li>
<li>where后的子查询用到了from中的表</li>
<li>用到了不可更新的视图</li>
</ol>
<h3 id="83存储过程">8.3、存储过程</h3>
<h4 id="831语法">8.3.1、语法</h4>
<p><strong>一、含义</strong></p>
<p>存储过程，类似于Java中的方法，它是一组预先编译好的SQL语句的集合，理解成批处理语句</p>
<p><strong>二、特点</strong></p>
<ol>
<li>简化sql语句</li>
<li>提高了sql的重用性</li>
<li>减少了编译次数并且减少了和数据库服务器的连接次数，提高了效率</li>
</ol>
<p><strong>三、语法</strong></p>
<p>1、创建</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">标准格式如下：</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> <span style="color:#a61717;background-color:#e3d2d2">存储过程名</span>(<span style="color:#a61717;background-color:#e3d2d2">参数模式</span> <span style="color:#a61717;background-color:#e3d2d2">参数名</span> <span style="color:#a61717;background-color:#e3d2d2">参数类型</span>,...)
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#a61717;background-color:#e3d2d2">存储过程体（一组合法的</span>SQL语句<span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">参数模式</span><span style="color:#000;font-weight:bold">in</span><span style="color:#a61717;background-color:#e3d2d2">：参数类型是输入的</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">参数模式</span><span style="color:#000;font-weight:bold">out</span><span style="color:#a61717;background-color:#e3d2d2">：参数类型是输出的</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">参数模式</span><span style="color:#000;font-weight:bold">inout</span><span style="color:#a61717;background-color:#e3d2d2">：参数类型既可以输入也可以输出</span>

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">调用</span>in模式的参数<span style="color:#a61717;background-color:#e3d2d2">：</span> <span style="color:#000;font-weight:bold">CALL</span> sp1(<span style="color:#d14">&#39;Hello,World&#39;</span>);
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">调用</span>out模式的参数<span style="color:#a61717;background-color:#e3d2d2">：</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@</span>name; <span style="color:#000;font-weight:bold">CALL</span> sp1(<span style="color:#000;font-weight:bold">@</span>name); <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@</span>name;
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">调用</span>inout模式的参数<span style="color:#a61717;background-color:#e3d2d2">：</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@</span>name<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#000;font-weight:bold">CALL</span> sp1(<span style="color:#000;font-weight:bold">@</span>name); <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@</span>name;
<span style="color:#099">123456789101112131415</span>
</code></pre></td></tr></table>
</div>
</div><p>2、修改</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">先删除，在创建</span>
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>3、删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> <span style="color:#a61717;background-color:#e3d2d2">存储过程名</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> <span style="color:#a61717;background-color:#e3d2d2">存储过程名</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="832变量">8.3.2、变量</h4>
<p><strong>一、分类</strong></p>
<ol>
<li>系统变量
<ul>
<li>全局变量</li>
<li>会话变量</li>
</ul>
</li>
<li>自定义变量
<ul>
<li>用户变量</li>
<li>局部变量</li>
</ul>
</li>
</ol>
<p><strong>二、语法</strong></p>
<p>1、全局变量</p>
<p>描述：服务器层面上的，必须拥有super权限才能为系统变量赋值，作用域为整个服务器，也就是针对于所有连接（会话）有效，但不能跨重启</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、查看所有系统变量</span>
<span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#000;font-weight:bold">GLOBAL</span> VARIABLES;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、查看满足条件的部分系统变量</span>
<span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#000;font-weight:bold">GLOBAL</span> VARIABLES <span style="color:#000;font-weight:bold">LIKE</span> <span style="color:#d14">&#39;%char%&#39;</span>;

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、查看指定的系统变量的值</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@@</span><span style="color:#000;font-weight:bold">global</span> <span style="color:#a61717;background-color:#e3d2d2">系统变量名</span>;

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、为某个系统变量赋值</span>
<span style="color:#a61717;background-color:#e3d2d2">方式一：</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">GLOBAL</span> <span style="color:#a61717;background-color:#e3d2d2">系统变量名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>;
<span style="color:#a61717;background-color:#e3d2d2">方式二：</span> <span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@@</span><span style="color:#000;font-weight:bold">global</span> <span style="color:#a61717;background-color:#e3d2d2">系统变量名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>;
<span style="color:#099">123456789101112</span>
</code></pre></td></tr></table>
</div>
</div><p>2、会话变量</p>
<p>描述：服务器为每一个连接的客户端都提供了系统变量，作用域为当前的连接（会话）</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、查看所有系统变量</span>
<span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">SESSION</span> <span style="color:#a61717;background-color:#e3d2d2">】</span> VARIABLES;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、查看满足条件的部分系统变量</span>
<span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">SESSION</span> <span style="color:#a61717;background-color:#e3d2d2">】</span> VARIABLES <span style="color:#000;font-weight:bold">LIKE</span> <span style="color:#d14">&#39;%char%&#39;</span>;

<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、查看指定的系统变量的值</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">SESSION</span> <span style="color:#a61717;background-color:#e3d2d2">】系统变量名</span>;

<span style="color:#099">4</span><span style="color:#a61717;background-color:#e3d2d2">、为某个系统变量赋值</span>
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">SESSION</span> <span style="color:#a61717;background-color:#e3d2d2">】系统变量名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>;
<span style="color:#099">1234567891011</span>
</code></pre></td></tr></table>
</div>
</div><p>3、用户变量</p>
<p>描述：针对于当前连接（会话）生效</p>
<p>位置：可以在begin end里面，也可以放在外面</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、声明并赋值</span>
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#a61717;background-color:#e3d2d2">或</span>
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span>:<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#a61717;background-color:#e3d2d2">或</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span>:<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、更新值</span>
<span style="color:#a61717;background-color:#e3d2d2">方式一：</span>
	<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#a61717;background-color:#e3d2d2">或</span>
	<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span>:<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#a61717;background-color:#e3d2d2">或</span>
	<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span>:<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>;
<span style="color:#a61717;background-color:#e3d2d2">方式二：</span>
	<span style="color:#000;font-weight:bold">SELECT</span> xxx <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表</span>;
	
<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、查看值</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span>;
<span style="color:#099">123456789101112131415</span>
</code></pre></td></tr></table>
</div>
</div><p>4、局部变量</p>
<p>描述：作用域：仅仅在定义它的begin end中有效
位置：只能放在begin end中，而且只能放在第一句</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">、声明并赋值</span>
<span style="color:#000;font-weight:bold">DECLARE</span> <span style="color:#a61717;background-color:#e3d2d2">变量名</span> <span style="color:#a61717;background-color:#e3d2d2">类型</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#a61717;background-color:#e3d2d2">值</span> <span style="color:#a61717;background-color:#e3d2d2">】</span>;

<span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">、更新值</span>
<span style="color:#a61717;background-color:#e3d2d2">方式一：</span>
	<span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">变量名</span><span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#a61717;background-color:#e3d2d2">或</span>
	<span style="color:#000;font-weight:bold">SET</span> <span style="color:#a61717;background-color:#e3d2d2">变量名</span>:<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>; <span style="color:#a61717;background-color:#e3d2d2">或</span>
	<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">@</span><span style="color:#a61717;background-color:#e3d2d2">变量名</span>:<span style="color:#000;font-weight:bold">=</span><span style="color:#a61717;background-color:#e3d2d2">值</span>;
<span style="color:#a61717;background-color:#e3d2d2">方式二：</span>
	<span style="color:#000;font-weight:bold">SELECT</span> xxx <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#a61717;background-color:#e3d2d2">变量名</span> <span style="color:#000;font-weight:bold">FROM</span> <span style="color:#a61717;background-color:#e3d2d2">表</span>;
	
<span style="color:#099">3</span><span style="color:#a61717;background-color:#e3d2d2">、查看值</span>
<span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">变量名</span>;
<span style="color:#099">12345678910111213</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="833分支结构">8.3.3、分支结构</h4>
<h5 id="8331if结构">8.3.3.1、if结构</h5>
<p><strong>一、语法</strong></p>
<p>注意：只能应用在begin end中</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">IF</span> <span style="color:#a61717;background-color:#e3d2d2">条件</span><span style="color:#099">1</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">1</span>;
ELSEIF <span style="color:#a61717;background-color:#e3d2d2">条件</span><span style="color:#099">2</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">2</span>;
ELSEIF <span style="color:#a61717;background-color:#e3d2d2">条件</span><span style="color:#099">3</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">3</span>;
....
<span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">ELSE</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span>n; <span style="color:#a61717;background-color:#e3d2d2">】</span>
<span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">IF</span>;
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、演示</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">创建函数，实现传入成绩，如果成绩</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">90</span>,<span style="color:#a61717;background-color:#e3d2d2">返回</span>A<span style="color:#a61717;background-color:#e3d2d2">，如果成绩</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">80</span>,<span style="color:#a61717;background-color:#e3d2d2">返回</span>B<span style="color:#a61717;background-color:#e3d2d2">，如果成绩</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">60</span>,<span style="color:#a61717;background-color:#e3d2d2">返回</span><span style="color:#000;font-weight:bold">C</span><span style="color:#a61717;background-color:#e3d2d2">，否则返回</span>D
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">FUNCTION</span> test_if(score <span style="color:#0086b3">FLOAT</span>) <span style="color:#000;font-weight:bold">RETURNS</span> <span style="color:#0086b3">CHAR</span>
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#000;font-weight:bold">DECLARE</span> ch <span style="color:#0086b3">CHAR</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#d14">&#39;A&#39;</span>;
	<span style="color:#000;font-weight:bold">IF</span> score<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">90</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;A&#39;</span>;
	ELSEIF score<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">80</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;B&#39;</span>;
	ELSEIF score<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">60</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;C&#39;</span>;
	<span style="color:#000;font-weight:bold">ELSE</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;D&#39;</span>;
	<span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">IF</span>;
	<span style="color:#000;font-weight:bold">RETURN</span> ch;
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">函数调用</span>
<span style="color:#000;font-weight:bold">SELECT</span> test_if(<span style="color:#099">87</span>)<span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#099">12345678910111213141516</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="8332case结构">8.3.3.2、case结构</h5>
<p><strong>一、语法</strong></p>
<p>注意：只能应用在begin end中</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">语法</span><span style="color:#099">1</span><span style="color:#a61717;background-color:#e3d2d2">：</span>
<span style="color:#000;font-weight:bold">CASE</span> <span style="color:#a61717;background-color:#e3d2d2">表达式或字段</span>
<span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#a61717;background-color:#e3d2d2">值</span><span style="color:#099">1</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">1</span>;
<span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#a61717;background-color:#e3d2d2">值</span><span style="color:#099">2</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">2</span>;
...
<span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">ELSE</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span>n; <span style="color:#a61717;background-color:#e3d2d2">】</span>
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">CASE</span> <span style="color:#a61717;background-color:#e3d2d2">】</span>;

<span style="color:#a61717;background-color:#e3d2d2">语法</span><span style="color:#099">2</span><span style="color:#a61717;background-color:#e3d2d2">：</span>
<span style="color:#000;font-weight:bold">CASE</span> 
<span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#a61717;background-color:#e3d2d2">条件</span><span style="color:#099">1</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">1</span>;
<span style="color:#000;font-weight:bold">WHEN</span> <span style="color:#a61717;background-color:#e3d2d2">条件</span><span style="color:#099">2</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span><span style="color:#099">2</span>;
...
<span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">ELSE</span> <span style="color:#a61717;background-color:#e3d2d2">语句</span>n; <span style="color:#a61717;background-color:#e3d2d2">】</span>
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">CASE</span> <span style="color:#a61717;background-color:#e3d2d2">】</span>;
<span style="color:#099">123456789101112131415</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>二、演示</strong></p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">创建函数，实现传入成绩，如果成绩</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">90</span>,<span style="color:#a61717;background-color:#e3d2d2">返回</span>A<span style="color:#a61717;background-color:#e3d2d2">，如果成绩</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">80</span>,<span style="color:#a61717;background-color:#e3d2d2">返回</span>B<span style="color:#a61717;background-color:#e3d2d2">，如果成绩</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">60</span>,<span style="color:#a61717;background-color:#e3d2d2">返回</span><span style="color:#000;font-weight:bold">C</span><span style="color:#a61717;background-color:#e3d2d2">，否则返回</span>D
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">FUNCTION</span> test_case (score <span style="color:#0086b3">FLOAT</span>) <span style="color:#000;font-weight:bold">RETURNS</span> <span style="color:#0086b3">CHAR</span> 
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#000;font-weight:bold">DECLARE</span> ch <span style="color:#0086b3">CHAR</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#d14">&#39;A&#39;</span> ;
	
	<span style="color:#000;font-weight:bold">CASE</span> 
	<span style="color:#000;font-weight:bold">WHEN</span> score<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">90</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;A&#39;</span>;
	<span style="color:#000;font-weight:bold">WHEN</span> score<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">80</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;B&#39;</span>;
	<span style="color:#000;font-weight:bold">WHEN</span> score<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">60</span> <span style="color:#000;font-weight:bold">THEN</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;C&#39;</span>;
	<span style="color:#000;font-weight:bold">ELSE</span> <span style="color:#000;font-weight:bold">SET</span> ch<span style="color:#000;font-weight:bold">=</span><span style="color:#d14">&#39;D&#39;</span>;
	<span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">CASE</span>;
	
	<span style="color:#000;font-weight:bold">RETURN</span> ch;
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">函数调用</span>
<span style="color:#000;font-weight:bold">SELECT</span> test_case(<span style="color:#099">56</span>);
<span style="color:#099">12345678910111213141516171819</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="834循环结构">8.3.4、循环结构</h4>
<p><strong>一、分类</strong></p>
<p>
        <a data-fancybox="gallery" href="https://i.loli.net/2020/12/24/ZksmP9fUDupIATC.png">
            <img class="mx-auto" alt="image-20200728162123754" src="https://i.loli.net/2020/12/24/ZksmP9fUDupIATC.png" />
        </a>
    </p>
<p><strong>二、演示</strong></p>
<h5 id="8341while结构">8.3.4.1、while结构</h5>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">批量插入，根据次数插入到</span>admin表中多条记录<span style="color:#a61717;background-color:#e3d2d2">，如果次数</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">20</span><span style="color:#a61717;background-color:#e3d2d2">则停止</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除过程</span>
<span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> <span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">EXISTS</span> test_while;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">定义过程</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> test_while(<span style="color:#000;font-weight:bold">IN</span> insertCount <span style="color:#0086b3">INT</span>)
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#000;font-weight:bold">DECLARE</span> i <span style="color:#0086b3">INT</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#099">1</span>;
	a:WHILE i<span style="color:#000;font-weight:bold">&lt;=</span>insertCount <span style="color:#000;font-weight:bold">DO</span>
		<span style="color:#000;font-weight:bold">#</span>LEAVE代表Java中的break关键字<span style="color:#a61717;background-color:#e3d2d2">；</span>ITERATE代表Java中的continue关键字
		<span style="color:#000;font-weight:bold">IF</span> i<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">20</span> <span style="color:#000;font-weight:bold">THEN</span> LEAVE a;
		<span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">IF</span>;
		<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#000;font-weight:bold">admin</span>(username,<span style="color:#000;font-weight:bold">`</span>password<span style="color:#000;font-weight:bold">`</span>) <span style="color:#000;font-weight:bold">VALUES</span>(CONCAT(<span style="color:#d14">&#39;xiaohua&#39;</span>,i),<span style="color:#d14">&#39;0000&#39;</span>);
		<span style="color:#000;font-weight:bold">SET</span> i<span style="color:#000;font-weight:bold">=</span>i<span style="color:#000;font-weight:bold">+</span><span style="color:#099">1</span>;
	<span style="color:#000;font-weight:bold">END</span> WHILE a;
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">调用过程</span>
<span style="color:#000;font-weight:bold">CALL</span> test_while(<span style="color:#099">100</span>);
<span style="color:#099">123456789101112131415161718192021</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="8342repeat结构">8.3.4.2、repeat结构</h5>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">批量插入，根据次数插入到</span>admin表中多条记录<span style="color:#a61717;background-color:#e3d2d2">，如果次数</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">20</span><span style="color:#a61717;background-color:#e3d2d2">则停止</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除过程</span>
<span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> <span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">EXISTS</span> test_repeat;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">定义过程</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> test_repeat(<span style="color:#000;font-weight:bold">IN</span> insertCount <span style="color:#0086b3">INT</span>)
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#000;font-weight:bold">DECLARE</span> i <span style="color:#0086b3">INT</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#099">1</span>;
	a:REPEAT 
		<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#000;font-weight:bold">admin</span>(username,<span style="color:#000;font-weight:bold">`</span>password<span style="color:#000;font-weight:bold">`</span>) <span style="color:#000;font-weight:bold">VALUES</span>(CONCAT(<span style="color:#d14">&#39;xiaohua&#39;</span>,i),<span style="color:#d14">&#39;0000&#39;</span>);
		<span style="color:#000;font-weight:bold">SET</span> i<span style="color:#000;font-weight:bold">=</span>i<span style="color:#000;font-weight:bold">+</span><span style="color:#099">1</span>;
	<span style="color:#000;font-weight:bold">UNTIL</span> i<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">20</span>		
	<span style="color:#000;font-weight:bold">END</span> REPEAT a;
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">调用过程</span>
<span style="color:#000;font-weight:bold">CALL</span> test_repeat(<span style="color:#099">100</span>);
<span style="color:#099">12345678910111213141516171819</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="8343loop结构">8.3.4.3、loop结构</h5>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">批量插入，根据次数插入到</span>admin表中多条记录<span style="color:#a61717;background-color:#e3d2d2">，如果次数</span><span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">20</span><span style="color:#a61717;background-color:#e3d2d2">则停止</span>
<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">删除过程</span>
<span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> <span style="color:#000;font-weight:bold">IF</span> <span style="color:#000;font-weight:bold">EXISTS</span> test_loop;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">定义过程</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> test_loop(<span style="color:#000;font-weight:bold">IN</span> insertCount <span style="color:#0086b3">INT</span>)
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#000;font-weight:bold">DECLARE</span> i <span style="color:#0086b3">INT</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#099">1</span>;
	a:LOOP 
		<span style="color:#000;font-weight:bold">#</span>LEAVE代表Java中的break关键字<span style="color:#a61717;background-color:#e3d2d2">；</span>ITERATE代表Java中的continue关键字
		<span style="color:#000;font-weight:bold">IF</span> i<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#099">20</span> <span style="color:#000;font-weight:bold">THEN</span> LEAVE a;
		<span style="color:#000;font-weight:bold">END</span> <span style="color:#000;font-weight:bold">IF</span>;
		<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#000;font-weight:bold">admin</span>(username,<span style="color:#000;font-weight:bold">`</span>password<span style="color:#000;font-weight:bold">`</span>) <span style="color:#000;font-weight:bold">VALUES</span>(CONCAT(<span style="color:#d14">&#39;xiaohua&#39;</span>,i),<span style="color:#d14">&#39;0000&#39;</span>);
		<span style="color:#000;font-weight:bold">SET</span> i<span style="color:#000;font-weight:bold">=</span>i<span style="color:#000;font-weight:bold">+</span><span style="color:#099">1</span>;
	<span style="color:#000;font-weight:bold">END</span> LOOP a;
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">调用过程</span>
<span style="color:#000;font-weight:bold">CALL</span> test_loop(<span style="color:#099">100</span>);
<span style="color:#099">123456789101112131415161718192021</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="835游标">8.3.5、游标</h4>
<p><strong>一、含义</strong></p>
<p>游标是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用光标对结果集进行循环的处理</p>
<p><strong>二、语法</strong></p>
<p>1、声明游标</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DECLARE</span> <span style="color:#a61717;background-color:#e3d2d2">游标名称</span> <span style="color:#000;font-weight:bold">CURSOR</span> <span style="color:#000;font-weight:bold">FOR</span> select结果集;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>2、打开游标</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">OPEN</span> <span style="color:#a61717;background-color:#e3d2d2">游标名称</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>3、获取游标</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">FETCH</span> <span style="color:#a61717;background-color:#e3d2d2">游标名称</span> <span style="color:#000;font-weight:bold">INTO</span> <span style="color:#a61717;background-color:#e3d2d2">变量列表（需要跟</span>select结果集的字段对应<span style="color:#a61717;background-color:#e3d2d2">）</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、关闭游标</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CLOSE</span> <span style="color:#a61717;background-color:#e3d2d2">游标名称</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>三、演示</strong></p>
<p>建数据表以用来演示</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> emp (
  id <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> AUTO_INCREMENT,
  NAME <span style="color:#0086b3">VARCHAR</span> (<span style="color:#099">50</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;姓名&#39;</span>,
  age <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;年龄&#39;</span>,
  salary <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;薪水&#39;</span>,
  <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span> (<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span>)
) ENGINE <span style="color:#000;font-weight:bold">=</span> INNODB <span style="color:#000;font-weight:bold">DEFAULT</span> CHARSET <span style="color:#000;font-weight:bold">=</span> utf8 ;

<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> emp(id,NAME,age,salary) 
<span style="color:#000;font-weight:bold">VALUES</span>
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;金毛狮王&#39;</span>,<span style="color:#099">55</span>,<span style="color:#099">3800</span>),
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;白眉鹰王&#39;</span>,<span style="color:#099">60</span>,<span style="color:#099">4000</span>),
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;青翼蝠王&#39;</span>,<span style="color:#099">38</span>,<span style="color:#099">2800</span>),
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;紫衫龙王&#39;</span>,<span style="color:#099">42</span>,<span style="color:#099">1800</span>);
<span style="color:#099">1234567891011121314</span>
</code></pre></td></tr></table>
</div>
</div><p>循环获取该表的内容</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">PROCEDURE</span> getAllRowsForEmpTable()
<span style="color:#000;font-weight:bold">BEGIN</span>
  <span style="color:#000;font-weight:bold">DECLARE</span> e_id <span style="color:#0086b3">INT</span>(<span style="color:#099">11</span>);
  <span style="color:#000;font-weight:bold">DECLARE</span> e_name <span style="color:#0086b3">VARCHAR</span>(<span style="color:#099">50</span>);
  <span style="color:#000;font-weight:bold">DECLARE</span> e_age <span style="color:#0086b3">INT</span>(<span style="color:#099">11</span>);
  <span style="color:#000;font-weight:bold">DECLARE</span> e_salary <span style="color:#0086b3">INT</span>(<span style="color:#099">11</span>);
  <span style="color:#000;font-weight:bold">DECLARE</span> has_data <span style="color:#0086b3">INT</span> <span style="color:#000;font-weight:bold">DEFAULT</span> <span style="color:#099">1</span>;
  
  <span style="color:#000;font-weight:bold">DECLARE</span> emp_result <span style="color:#000;font-weight:bold">CURSOR</span> <span style="color:#000;font-weight:bold">FOR</span> <span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#000;font-weight:bold">*</span> <span style="color:#000;font-weight:bold">FROM</span> emp;
  <span style="color:#000;font-weight:bold">DECLARE</span> EXIT <span style="color:#000;font-weight:bold">HANDLER</span> <span style="color:#000;font-weight:bold">FOR</span> <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">FOUND</span> <span style="color:#000;font-weight:bold">SET</span> has_data<span style="color:#000;font-weight:bold">=</span><span style="color:#099">0</span>;
  
  <span style="color:#000;font-weight:bold">OPEN</span> emp_result;
  
  REPEAT
    <span style="color:#000;font-weight:bold">FETCH</span> emp_result <span style="color:#000;font-weight:bold">INTO</span> e_id,e_name,e_age,e_salary;
    <span style="color:#000;font-weight:bold">SELECT</span> CONCAT(<span style="color:#d14">&#39;id=&#39;</span>,e_id , <span style="color:#d14">&#39;, name=&#39;</span>,e_name, <span style="color:#d14">&#39;, age=&#39;</span>, e_age, <span style="color:#d14">&#39;, salary=&#39;</span>,e_salary) <span style="color:#000;font-weight:bold">as</span> emp_info;
    <span style="color:#000;font-weight:bold">UNTIL</span> has_data <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">0</span>
  <span style="color:#000;font-weight:bold">END</span> REPEAT;
  
  <span style="color:#000;font-weight:bold">CLOSE</span> emp_result;
<span style="color:#000;font-weight:bold">END</span><span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ; 
<span style="color:#099">1234567891011121314151617181920212223</span>
</code></pre></td></tr></table>
</div>
</div><p>打开命令行调用查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CALL</span> getAllRowsForEmpTable();
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="84函数">8.4、函数</h3>
<p><strong>一、含义</strong></p>
<p>其实函数就是一个有返回值的过程</p>
<p><strong>二、语法</strong></p>
<p>1、创建</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">8
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">FUNCTION</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>(<span style="color:#a61717;background-color:#e3d2d2">参数名</span> <span style="color:#a61717;background-color:#e3d2d2">参数类型</span>) 
<span style="color:#000;font-weight:bold">RETURNS</span> <span style="color:#a61717;background-color:#e3d2d2">返回类型</span>
<span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#a61717;background-color:#e3d2d2">函数过程体（一组合法的</span>SQL语句<span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;
<span style="color:#099">1234567</span>
</code></pre></td></tr></table>
</div>
</div><p>2、修改</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">先删除，在创建</span>
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>3、删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">FUNCTION</span> <span style="color:#a61717;background-color:#e3d2d2">函数名；</span>
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SHOW</span> <span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">FUNCTION</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>5、调用</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SELECT</span> <span style="color:#a61717;background-color:#e3d2d2">函数名</span>(<span style="color:#a61717;background-color:#e3d2d2">实参列表</span>);
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>三、注意</strong></p>
<ol>
<li>函数体中肯定需要有return语句</li>
<li>存储过程和函数的区别在于函数必须有返回值，而存储过程没有</li>
</ol>
<h3 id="85触发器">8.5、触发器</h3>
<p><strong>一、含义</strong></p>
<p>MySQL在5.0以后推出了触发器（TRIGGER），触发器是与表有关的数据库对象，指在 insert/update/delete 之前或之后，触发并执行触发器中定义的SQL语句集合。触发器的这种特性可以协助应用在数据库端确保数据的完整性、日志记录 、数据校验等操作。使用别名 OLD 和 NEW 来引用触发器中发生变化的记录内容，这与其他的数据库是相似的。现在MySQL触发器还只支持行级触发，不支持语句级触发</p>
<p><strong>二、分类</strong></p>
<table>
<thead>
<tr>
<th>触发器类型</th>
<th>NEW 和 OLD的使用</th>
</tr>
</thead>
<tbody>
<tr>
<td>INSERT 型触发器</td>
<td>NEW 表示将要或者已经新增的数据</td>
</tr>
<tr>
<td>UPDATE 型触发器</td>
<td>OLD 表示修改之前的数据 , NEW 表示将要或已经修改后的数据</td>
</tr>
<tr>
<td>DELETE 型触发器</td>
<td>OLD 表示将要或者已经删除的数据</td>
</tr>
</tbody>
</table>
<p><strong>三、语法</strong></p>
<p>1、创建</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TRIGGER</span> <span style="color:#a61717;background-color:#e3d2d2">触发器名称</span> 
<span style="color:#000;font-weight:bold">BEFORE</span><span style="color:#000;font-weight:bold">/</span><span style="color:#000;font-weight:bold">AFTER</span> <span style="color:#000;font-weight:bold">INSERT</span><span style="color:#000;font-weight:bold">/</span><span style="color:#000;font-weight:bold">UPDATE</span><span style="color:#000;font-weight:bold">/</span><span style="color:#000;font-weight:bold">DELETE</span>
<span style="color:#000;font-weight:bold">ON</span> <span style="color:#a61717;background-color:#e3d2d2">表名称</span> 
<span style="color:#a61717;background-color:#e3d2d2">【</span> <span style="color:#000;font-weight:bold">FOR</span> <span style="color:#000;font-weight:bold">EACH</span> <span style="color:#000;font-weight:bold">ROW</span> <span style="color:#a61717;background-color:#e3d2d2">】</span><span style="color:#998;font-style:italic">--行级触发器
</span><span style="color:#998;font-style:italic"></span><span style="color:#000;font-weight:bold">BEGIN</span>
	<span style="color:#a61717;background-color:#e3d2d2">触发器过程体（一组合法的</span>SQL语句<span style="color:#a61717;background-color:#e3d2d2">）</span>
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p>2、更新</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#a61717;background-color:#e3d2d2">先删除，在创建</span>
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>3、删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> <span style="color:#000;font-weight:bold">TRIGGER</span> <span style="color:#a61717;background-color:#e3d2d2">触发器名称</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SHOW</span> TRIGGERS;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>四、演示</strong></p>
<p>创建数据表以用来演示</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> emp (
  id <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> AUTO_INCREMENT,
  NAME <span style="color:#0086b3">VARCHAR</span> (<span style="color:#099">50</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;姓名&#39;</span>,
  age <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;年龄&#39;</span>,
  salary <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;薪水&#39;</span>,
  <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span> (<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span>)
) ENGINE <span style="color:#000;font-weight:bold">=</span> INNODB <span style="color:#000;font-weight:bold">DEFAULT</span> CHARSET <span style="color:#000;font-weight:bold">=</span> utf8 ;
<span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> emp(id,NAME,age,salary) 
<span style="color:#000;font-weight:bold">VALUES</span>
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;金毛狮王&#39;</span>,<span style="color:#099">55</span>,<span style="color:#099">3800</span>),
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;白眉鹰王&#39;</span>,<span style="color:#099">60</span>,<span style="color:#099">4000</span>),
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;青翼蝠王&#39;</span>,<span style="color:#099">38</span>,<span style="color:#099">2800</span>),
(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;紫衫龙王&#39;</span>,<span style="color:#099">42</span>,<span style="color:#099">1800</span>);

<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TABLE</span> emp_logs (
  id <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> AUTO_INCREMENT,
  <span style="color:#000;font-weight:bold">operation</span> <span style="color:#0086b3">VARCHAR</span> (<span style="color:#099">20</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;操作类型, insert/update/delete&#39;</span>,
  operate_time DATETIME <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;操作时间&#39;</span>,
  operate_id <span style="color:#0086b3">INT</span> (<span style="color:#099">11</span>) <span style="color:#000;font-weight:bold">NOT</span> <span style="color:#000;font-weight:bold">NULL</span> <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;操作表的ID&#39;</span>,
  operate_params <span style="color:#0086b3">VARCHAR</span> (<span style="color:#099">500</span>) <span style="color:#000;font-weight:bold">COMMENT</span> <span style="color:#d14">&#39;操作参数&#39;</span>,
  <span style="color:#000;font-weight:bold">PRIMARY</span> <span style="color:#000;font-weight:bold">KEY</span> (<span style="color:#000;font-weight:bold">`</span>id<span style="color:#000;font-weight:bold">`</span>)
) ENGINE <span style="color:#000;font-weight:bold">=</span> INNODB <span style="color:#000;font-weight:bold">DEFAULT</span> CHARSET <span style="color:#000;font-weight:bold">=</span> utf8 ;
<span style="color:#099">12345678910111213141516171819202122</span>
</code></pre></td></tr></table>
</div>
</div><p>编写触发器进行测试</p>
<p>创建 insert 型触发器，完成插入数据时的日志记录：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TRIGGER</span> emp_logs_insert_trigger
<span style="color:#000;font-weight:bold">AFTER</span> <span style="color:#000;font-weight:bold">INSERT</span> 
<span style="color:#000;font-weight:bold">ON</span> emp 
<span style="color:#000;font-weight:bold">FOR</span> <span style="color:#000;font-weight:bold">EACH</span> <span style="color:#000;font-weight:bold">ROW</span> 
<span style="color:#000;font-weight:bold">BEGIN</span>
  <span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> emp_logs (id,<span style="color:#000;font-weight:bold">operation</span>,operate_time,operate_id,operate_params) <span style="color:#000;font-weight:bold">VALUES</span>(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;insert&#39;</span>,NOW(),<span style="color:#000;font-weight:bold">new</span>.id,CONCAT(<span style="color:#d14">&#39;插入后(id:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.id,<span style="color:#d14">&#39;, name:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.name,<span style="color:#d14">&#39;, age:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.age,<span style="color:#d14">&#39;, salary:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.salary,<span style="color:#d14">&#39;)&#39;</span>));	
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p>创建 update 型触发器，完成更新数据时的日志记录：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TRIGGER</span> emp_logs_update_trigger
<span style="color:#000;font-weight:bold">AFTER</span> <span style="color:#000;font-weight:bold">UPDATE</span> 
<span style="color:#000;font-weight:bold">ON</span> emp 
<span style="color:#000;font-weight:bold">FOR</span> <span style="color:#000;font-weight:bold">EACH</span> <span style="color:#000;font-weight:bold">ROW</span> 
<span style="color:#000;font-weight:bold">BEGIN</span>
  <span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> emp_logs (id,<span style="color:#000;font-weight:bold">operation</span>,operate_time,operate_id,operate_params) <span style="color:#000;font-weight:bold">VALUES</span>(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;update&#39;</span>,NOW(),<span style="color:#000;font-weight:bold">new</span>.id,CONCAT(<span style="color:#d14">&#39;修改前(id:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.id,<span style="color:#d14">&#39;, name:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.name,<span style="color:#d14">&#39;, age:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.age,<span style="color:#d14">&#39;, salary:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.salary,<span style="color:#d14">&#39;) , 修改后(id&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.id, <span style="color:#d14">&#39;name:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.name,<span style="color:#d14">&#39;, age:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.age,<span style="color:#d14">&#39;, salary:&#39;</span>,<span style="color:#000;font-weight:bold">new</span>.salary,<span style="color:#d14">&#39;)&#39;</span>));                                                                      
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p>创建delete 行的触发器 , 完成删除数据时的日志记录：</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> <span style="color:#000;font-weight:bold">TRIGGER</span> emp_logs_delete_trigger
<span style="color:#000;font-weight:bold">AFTER</span> <span style="color:#000;font-weight:bold">DELETE</span> 
<span style="color:#000;font-weight:bold">ON</span> emp 
<span style="color:#000;font-weight:bold">FOR</span> <span style="color:#000;font-weight:bold">EACH</span> <span style="color:#000;font-weight:bold">ROW</span> 
<span style="color:#000;font-weight:bold">BEGIN</span>
  <span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> emp_logs (id,<span style="color:#000;font-weight:bold">operation</span>,operate_time,operate_id,operate_params) <span style="color:#000;font-weight:bold">VALUES</span>(<span style="color:#000;font-weight:bold">NULL</span>,<span style="color:#d14">&#39;delete&#39;</span>,NOW(),<span style="color:#000;font-weight:bold">old</span>.id,CONCAT(<span style="color:#d14">&#39;删除前(id:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.id,<span style="color:#d14">&#39;, name:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.name,<span style="color:#d14">&#39;, age:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.age,<span style="color:#d14">&#39;, salary:&#39;</span>,<span style="color:#000;font-weight:bold">old</span>.salary,<span style="color:#d14">&#39;)&#39;</span>));                                                                      
<span style="color:#000;font-weight:bold">END</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;
<span style="color:#099">123456789</span>
</code></pre></td></tr></table>
</div>
</div><p>依次调用以下的语句，查看emp_logs是否有触发器保存的日志记录</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">INSERT</span> <span style="color:#000;font-weight:bold">INTO</span> emp(id,NAME,age,salary) <span style="color:#000;font-weight:bold">VALUES</span>(<span style="color:#000;font-weight:bold">NULL</span>, <span style="color:#d14">&#39;光明左使&#39;</span>,<span style="color:#099">30</span>,<span style="color:#099">3500</span>);

<span style="color:#000;font-weight:bold">UPDATE</span> emp <span style="color:#000;font-weight:bold">SET</span> age <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">39</span> <span style="color:#000;font-weight:bold">WHERE</span> id <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">3</span>;

<span style="color:#000;font-weight:bold">DELETE</span> <span style="color:#000;font-weight:bold">FROM</span> emp <span style="color:#000;font-weight:bold">WHERE</span> id <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">5</span>;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="86事件">8.6、事件</h3>
<p><strong>一、含义</strong></p>
<p>MySQL在5.1以后推出了事件调度器（Event Scheduler），与triggers的事件触发不同，event类似与linux crontab计划任务，用于时间触发</p>
<p><strong>二、特点</strong></p>
<ol>
<li>事件是一组SQL集合，简单说就是mysql中的定时器，时间到了就执行</li>
<li>事件由一个特定的线程来管理的，也就是所谓的事件调度器，但是事件不能直接调用，通过单独或调用存储过程使用，在某一特定的时间点，触发相关的SQL语句或存储过程</li>
</ol>
<p><strong>三、语法</strong></p>
<p>1、创建</p>
<p>MySQL事件调度器event_scheduler负责调用事件，它默认是关闭的。这个调度器不断地监视一个事件是否要调用， 要创建事件，必须打开调度器。</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">28
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">29
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">30
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">31
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">32
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">33
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">34
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">35
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">36
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">37
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">38
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">39
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">40
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">41
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">42
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">43
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">44
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">开启事件调度器（任意一个都可以）</span>
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">GLOBAL</span> event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">ON</span>; 
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@@</span><span style="color:#000;font-weight:bold">global</span>.event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">ON</span>; 
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">GLOBAL</span> event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">1</span>; 
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@@</span><span style="color:#000;font-weight:bold">global</span>.event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">1</span>; 

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">关闭事件调度器（任意一个都可以）</span>
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">GLOBAL</span> event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">OFF</span>; 
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@@</span><span style="color:#000;font-weight:bold">global</span>.event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">OFF</span>; 
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">GLOBAL</span> event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">0</span>; 
<span style="color:#000;font-weight:bold">SET</span> <span style="color:#000;font-weight:bold">@@</span><span style="color:#000;font-weight:bold">global</span>.event_scheduler <span style="color:#000;font-weight:bold">=</span> <span style="color:#099">0</span>;
<span style="color:#099">1234567891011</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> <span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">CREATE</span> EVENT <span style="color:#a61717;background-color:#e3d2d2">事件名称</span>
<span style="color:#000;font-weight:bold">ON</span> SCHEDULE <span style="color:#a61717;background-color:#e3d2d2">执行时间和频率</span>
<span style="color:#998;font-style:italic">-- 执行时间和频率有两种形式AT和EVERY
</span><span style="color:#998;font-style:italic">-- 	使用 AT     关键字只会执行一次，格式如下：
</span><span style="color:#998;font-style:italic">-- 	AT CURRENT_TIMESTAMP 或者 &#39;YYYY-MM-DD HH:MM.SS&#39;
</span><span style="color:#998;font-style:italic">-- 	使用 EVERY  关键字指定时间间隔，格式如下：
</span><span style="color:#998;font-style:italic">-- 	EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]
</span><span style="color:#998;font-style:italic"></span>
<span style="color:#998;font-style:italic">-- 可选项，使用 STARTS 关键字指定开始时间，格式如下：
</span><span style="color:#998;font-style:italic">-- STARTS CURRENT_TIMESTAMP 或者 &#39;YYYY-MM-DD HH:MM.SS&#39;
</span><span style="color:#998;font-style:italic">-- 可选项，使用 ENDS   关键字指定结束时间，格式如下：
</span><span style="color:#998;font-style:italic">-- ENDS CURRENT_TIMESTAMP 或者 &#39;YYYY-MM-DD HH:MM.SS&#39;
</span><span style="color:#998;font-style:italic"></span>
<span style="color:#998;font-style:italic">-- [ON COMPLETION [NOT] PRESERVE] 
</span><span style="color:#998;font-style:italic">-- 可选项，设定event的生命
</span><span style="color:#998;font-style:italic">--		ON COMPLETION NOT PRESERVE ：即计划任务执行完毕后自动drop该事件（默认）
</span><span style="color:#998;font-style:italic">--		ON COMPLETION PRESERVE     ：即计划任务执行完毕后不会drop该事件
</span><span style="color:#998;font-style:italic"></span>
<span style="color:#998;font-style:italic">-- [ENABLE | DISABLE] 
</span><span style="color:#998;font-style:italic">-- 可选项，设定event的状态
</span><span style="color:#998;font-style:italic">--		ENABLE	：表示系统尝试执行这个事件（默认）
</span><span style="color:#998;font-style:italic">--		DISABLE	：表示系统尝试关闭这个事件
</span><span style="color:#998;font-style:italic"></span>
<span style="color:#998;font-style:italic">-- [COMMENT &#39;comment&#39;] 
</span><span style="color:#998;font-style:italic">-- 可选项，设定event的备注
</span><span style="color:#998;font-style:italic"></span><span style="color:#000;font-weight:bold">DO</span>
<span style="color:#000;font-weight:bold">BEGIN</span>
        <span style="color:#998;font-style:italic">/* 在这里写SQL语句或者调用存储过程 */</span>
<span style="color:#000;font-weight:bold">END</span><span style="color:#a61717;background-color:#e3d2d2">$</span>
<span style="color:#000;font-weight:bold">DELIMITER</span> ;
<span style="color:#099">12345678910111213141516171819202122232425262728293031</span>
</code></pre></td></tr></table>
</div>
</div><p>2、修改</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">先删除<span style="color:#a61717;background-color:#e3d2d2">，</span>在创建
1
</code></pre></td></tr></table>
</div>
</div><p>3、删除</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">DROP</span> EVENT <span style="color:#a61717;background-color:#e3d2d2">事件名称</span>;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>4、查看</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">SHOW</span> EVENTS;
<span style="color:#099">1</span>
</code></pre></td></tr></table>
</div>
</div><p>5、调用</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">启用事件</span>
<span style="color:#000;font-weight:bold">ALTER</span> EVENT <span style="color:#a61717;background-color:#e3d2d2">事件名称</span> ENABLE;

<span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">禁用事件</span>
<span style="color:#000;font-weight:bold">ALTER</span> EVENT <span style="color:#a61717;background-color:#e3d2d2">事件名称</span> DISABLE;
<span style="color:#099">12345</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>四、注意</strong></p>
<ol>
<li>默认创建事件存储在当前库中，也可显示指定事件创建在哪个库中</li>
<li>通过show events只能查看当前库中创建的事件</li>
<li>事件执行完即释放，如立即执行事件，执行完后，事件便自动删除，多次调用事件或等待执行事件可以查看到</li>
<li>如果两个事件需要在同一时刻调用，mysql会确定调用他们的顺序，如果要指定顺序，需要确保一个事件至少在另一个事件1秒后执行</li>
<li>对于递归调度的事件，结束日期不能在开始日期之前</li>
<li>select可以包含在一个事件中，然而他的结果消失了，就好像没执行过</li>
</ol>
<h2 id="第九章-数据备份与恢复">第九章 数据备份与恢复</h2>
<h3 id="91数据导出">9.1、数据导出</h3>
<p>1、导出某张数据表</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">格式：</span>mysqldump <span style="color:#000;font-weight:bold">-</span>u账户 <span style="color:#000;font-weight:bold">-</span>p密码 <span style="color:#a61717;background-color:#e3d2d2">数据库名</span> <span style="color:#a61717;background-color:#e3d2d2">数据表名</span> <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#a61717;background-color:#e3d2d2">文件保存路径</span>
mysqldump <span style="color:#000;font-weight:bold">-</span>uroot <span style="color:#000;font-weight:bold">-</span>p123456 test emp <span style="color:#000;font-weight:bold">&gt;</span> D:<span style="color:#000;font-weight:bold">/</span>emp.<span style="color:#000;font-weight:bold">sql</span>
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><p>2、导出整个数据库</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">格式：</span>mysqldump <span style="color:#000;font-weight:bold">-</span>u账户 <span style="color:#000;font-weight:bold">-</span>p密码 <span style="color:#a61717;background-color:#e3d2d2">数据库名</span> <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#a61717;background-color:#e3d2d2">文件保存路径</span>
mysqldump <span style="color:#000;font-weight:bold">-</span>uroot <span style="color:#000;font-weight:bold">-</span>p123456 test <span style="color:#000;font-weight:bold">&gt;</span> D:<span style="color:#000;font-weight:bold">/</span>test.<span style="color:#000;font-weight:bold">sql</span>
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="92数据导入">9.2、数据导入</h3>
<p>1、导入某张数据表</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">7
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">格式：</span>mysql <span style="color:#000;font-weight:bold">-</span>u账户 <span style="color:#000;font-weight:bold">-</span>p密码
<span style="color:#000;font-weight:bold">#</span>     use <span style="color:#a61717;background-color:#e3d2d2">数据库名</span>;
<span style="color:#000;font-weight:bold">#</span>     <span style="color:#000;font-weight:bold">source</span> <span style="color:#a61717;background-color:#e3d2d2">文件保存路径</span>
mysql <span style="color:#000;font-weight:bold">-</span>uroot <span style="color:#000;font-weight:bold">-</span>p123456
<span style="color:#000;font-weight:bold">&gt;</span>use test;
<span style="color:#000;font-weight:bold">&gt;</span><span style="color:#000;font-weight:bold">source</span> D:<span style="color:#000;font-weight:bold">/</span>emp.<span style="color:#000;font-weight:bold">sql</span>
<span style="color:#099">123456</span>
</code></pre></td></tr></table>
</div>
</div><p>2、导入整个数据库</p>
<div class="highlight"><div style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#000;font-weight:bold">#</span><span style="color:#a61717;background-color:#e3d2d2">格式：</span>mysql <span style="color:#000;font-weight:bold">-</span>u账户 <span style="color:#000;font-weight:bold">-</span>p密码 <span style="color:#000;font-weight:bold">&lt;</span> <span style="color:#a61717;background-color:#e3d2d2">文件保存路径</span>
mysql <span style="color:#000;font-weight:bold">-</span>uroot <span style="color:#000;font-weight:bold">-</span>p123456 <span style="color:#000;font-weight:bold">&lt;</span> test.<span style="color:#000;font-weight:bold">sql</span>
<span style="color:#099">12</span>
</code></pre></td></tr></table>
</div>
</div>
        </div>

        


        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/Mysql-%E6%95%B0%E6%8D%AE%E5%BA%93/Mysql%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84%E7%AE%A1%E7%90%86/">Mysql体系结构管理</a></li>
        
        <li><a href="/post/Mysql-%E6%95%B0%E6%8D%AE%E5%BA%93/my.cnf-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/">my.cnf 配置文件详解</a></li>
        
        <li><a href="/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/">Linux 基础配置 </a></li>
        
        <li><a href="/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/linux-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4-/">Linux 常用命令 </a></li>
        
        <li><a href="/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/Linux%E6%96%87%E4%BB%B6%E7%89%B9%E6%AE%8A%E6%9D%83%E9%99%90SUIDSGID%E4%B8%8ESBIT/">Linux文件特殊权限 </a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/Mysql'>Mysql</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://gitmatrix.gitee.io/">matrix By matrix</a>
        
    </div>
    <br />
    
    <div>
        <div class="github-badge">
            <a href="https://gohugo.io/" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">Hugo</span></a>
        </div>
        
        <div class="github-badge">
            <a href="https://github.com/flysnow-org/maupassant-hugo" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">Maupassant</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://gitmatrix.gitee.io/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://gitmatrix.gitee.io/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/" title="Linux 基础配置 ">Linux 基础配置 </a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/linux-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4-/" title="Linux 常用命令 ">Linux 常用命令 </a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/Linux%E6%96%87%E4%BB%B6%E7%89%B9%E6%AE%8A%E6%9D%83%E9%99%90SUIDSGID%E4%B8%8ESBIT/" title="Linux文件特殊权限 ">Linux文件特殊权限 </a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/Linux-%E7%A3%81%E7%9B%98%E6%8C%82%E8%BD%BD/" title="Liunx 磁盘分区与文件挂载 ">Liunx 磁盘分区与文件挂载 </a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Mysql-%E6%95%B0%E6%8D%AE%E5%BA%93/my.cnf-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/" title="my.cnf 配置文件详解">my.cnf 配置文件详解</a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Mysql-%E6%95%B0%E6%8D%AE%E5%BA%93/Mysql-%E6%93%8D%E4%BD%9C/" title="Mysql 基本操作">Mysql 基本操作</a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Mysql-%E6%95%B0%E6%8D%AE%E5%BA%93/Mysql%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84%E7%AE%A1%E7%90%86/" title="Mysql体系结构管理">Mysql体系结构管理</a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/RAID-%E7%A3%81%E7%9B%98%E9%98%B5%E5%88%97/" title="RAID 磁盘阵列配置 ">RAID 磁盘阵列配置 </a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/Linux-%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/rpm-%E4%B8%8E-yum-/" title="RPM 与 Yum ">RPM 与 Yum </a>
    </li>
    
    <li>
        <a href="https://gitmatrix.gitee.io/post/SSH/SSH-%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" title="SSH 基本操作">SSH 基本操作</a>
    </li>
    
</ul>
    </section>

    
<section class="widget">
    <h3 class="widget-title" style="color:#6E718A">课程直达</h3>
    <ul class="widget-list">
        
        <li>
            <a href="https://www.bilibili.com/video/BV1nA411h7C9" title="Burpsuite使用教程" target="_blank" style="color:#6E718A">
                
                    <img src="https://gitee.com/gitmatrix/images/raw/master/img/20210116152721.png">
                
            </a>
        </li>
        
        <li>
            <a href="https://www.bilibili.com/video/BV1yf4y1i7Pb?p=1" title="网络安全提升技术与渗透测试原理深度解析" target="_blank" style="color:#6E718A">
                
                    <img src="https://gitee.com/gitmatrix/images/raw/master/img/6f19fe2958f4d3c5edaee825cda334bac074abc8.jpg">
                
            </a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://gitmatrix.gitee.io/categories/Liunx/">Liunx (6)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/Mysql/">Mysql (3)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/SSH/">SSH (4)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/Test/">Test (1)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/Web-%E5%AE%89%E5%85%A8/">Web 安全 (9)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/web%E5%9F%BA%E7%A1%80/">web基础 (1)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86/">信息收集 (10)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/%E5%8D%81%E5%A4%A7%E6%BC%8F%E6%B4%9E/">十大漏洞 (8)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/%E6%95%99%E7%A8%8B/">教程 (1)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E6%B3%95/">网络安全法 (1)</a></li>
    
    <li><a href="https://gitmatrix.gitee.io/categories/%E8%BF%90%E7%BB%B4/">运维 (2)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://gitmatrix.gitee.io/tags/CSRF/">CSRF</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Chrome-Extension/">Chrome Extension</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Chrome%E6%8F%92%E4%BB%B6/">Chrome插件</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Google-hacking/">Google hacking</a>
    
    <a href="https://gitmatrix.gitee.io/tags/JavaScript/">JavaScript</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Linux/">Linux</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Mysql/">Mysql</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Payload/">Payload</a>
    
    <a href="https://gitmatrix.gitee.io/tags/XSS/">XSS</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Zabbix/">Zabbix</a>
    
    <a href="https://gitmatrix.gitee.io/tags/js/">js</a>
    
    <a href="https://gitmatrix.gitee.io/tags/Mysql/">Mysql</a>
    
    <a href="https://gitmatrix.gitee.io/tags/sql/">sql</a>
    
    <a href="https://gitmatrix.gitee.io/tags/ssh/">ssh</a>
    
    <a href="https://gitmatrix.gitee.io/tags/test/">test</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E6%B3%95/">中华人民共和国网络安全法</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E5%9F%BA%E6%9C%AC%E4%BF%A1%E6%81%AF%E6%94%B6%E9%9B%86/">基本信息收集</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%8C%87%E7%BA%B9%E8%AF%86%E5%88%AB/">指纹识别</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%94%B6%E9%9B%86%E5%AD%90%E5%9F%9F%E4%BF%A1%E6%81%AF/">收集子域信息</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%94%B6%E9%9B%86%E6%95%8F%E6%84%9F%E7%9B%AE%E5%BD%95%E6%96%87%E4%BB%B6/">收集敏感目录文件</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%94%BB%E5%87%BB/">攻击</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%95%99%E7%A8%8B/">教程</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%95%B4%E7%AB%99%E5%88%86%E6%9E%90/">整站分析</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%96%87%E4%BB%B6/">文件</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%B8%97%E9%80%8F%E6%B5%8B%E8%AF%95%E4%BB%8B%E7%BB%8D/">渗透测试介绍</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%B8%97%E9%80%8F%E6%B5%8B%E8%AF%95%E6%B5%81%E7%A8%8B/">渗透测试流程</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E6%BC%8F%E6%B4%9E/">漏洞</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E7%9C%9F%E5%AE%9EIP%E5%9C%B0%E5%9D%80%E6%9F%A5%E8%AF%A2/">真实IP地址查询</a>
    
    <a href="https://gitmatrix.gitee.io/tags/%E9%9D%B6%E5%9C%BA/">靶场</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://www.bugbank.cn/" title="漏洞银行">漏洞银行</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.xf1433.com/59.html" title="电脑黑客书籍在线阅读">电脑黑客书籍在线阅读</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://gitmatrix.gitee.io/index.xml"></a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>